简体   繁体   中英

How to deserialize json with unknown properties using JMS Serializer?

I'm trying to create DTO which will help me to deserialize nested JSON. I know the structure of JSON but on some nested step I don't know the names/indexes of JSON properties.

JSON looks like:

{
  "project": {
    "layer": {
      "480x960": [],
      "720x360": [],
      "...": [],
      "...": []
    }
  }
}

but I don't know names of properties inside the layer property.

I have some DTOs so far

Project DTO:

namespace App\DTO;

use JMS\Serializer\Annotation as Serializer;

class Project
{
    /**
     * @var Layer
     *
     * @Serializer\Type("App\DTO\Layer")
     */
    protected $layer;
}

Layer DTO: (which I stuck on)

namespace App\DTO;

use Doctrine\Common\Collections\Collection;
use JMS\Serializer\Annotation as Serializer;

class Layer
{
    // ???
}

I seek to achieve something like: $project->getLayer() will return Collection of objects ['480x960', '720x360', '...', etc.] . I don't bother to keep names/indexes of those unknown properties inside layer .

Try this. You don't need class for Layer.

use JMS\Serializer\Annotation as Serializer;

class Project
{
    /**
     * @Serializer\Type("array<string, array>")
     */
    protected $layer;
}

Check http://www.inanzzz.com/index.php/post/vsb9/mapping-random-json-and-xml-request-properties-with-jmsserializerbundle-in-symfony

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM