Disable multidimensional JSON API response using JSMSerializerBundle

I am working with Symfony2 / JSMSerializerBundle.

Serializing json flat objects for PHP objects works fine. But the API I'm using gives a multi-dimensional Json response:

{
"Webmessage": {
    "@version": "1.0",
    "Header": {
        "Country": "NL",
        "Language": "NL"
    },
    "Content": {
        "Filters": {
            "Sizes": {
                "Size": [
                    {
                        "@id": "241",
                        "#text": "3,5"
                    },
                    {
                        "@id": "55",
                        "#text": "36"
                    }
                ]
            },
            "Colours": {
                "Colour": [
                    {
                        "@id": "159",
                        "#text": "wit"
                    },
                    {
                        "@id": "54",
                        "#text": "zwart"
                    }
                ]
            }
        }
    }
}

}

As deserialized PHP, I want something like this:

Array
(
[sizes] => Array
    (
      [0] => AppBundle\Entity\Filter Object
      (
        [id:AppBundle\Entity\Filter:private] => 1
        [text:AppBundle\Entity\Filter:private] => Heren
       )

      [1] => AppBundle\Entity\Filter Object
      (
        [id:AppBundle\Entity\Filter:private] => 2
        [text:AppBundle\Entity\Filter:private] => Dames
      )
    )

[colour] => Array
    (
      [0] => AppBundle\Entity\Filter Object
      (
        [id:AppBundle\Entity\Filter:private] =>56
        [text:AppBundle\Entity\Filter:private] => Black
       )

      [1] => AppBundle\Entity\Filter Object
      (
        [id:AppBundle\Entity\Filter:private] => 212
        [text:AppBundle\Entity\Filter:private] => Yellow
      )
    )

)

Anyone have any tips on how I can do this?

Thank!

+4
source share
1 answer

Perhaps you can decode it first and then use the Normalizer to create objects. Something like that:

$array= json_decode($json, true);
$valueToDenormalize = $array['value'];

$normalizer = new GetSetMethodNormalizer();
$entity = $normalizer->denormalize($valueToDenormalize, 'Your\Class');

, . , , , Symfony.

Serializer: http://symfony.com/doc/current/components/serializer.html

: http://api.symfony.com/2.3/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.html

- json_encode: http://php.net/manual/en/function.json-decode.php

+1

All Articles