Symfony - Deserialize json to an array of entities

有些话、适合烂在心里 提交于 2019-11-30 01:57:36

The error is pretty clear. Your string does not match any existant class.

The example in official documentation says:

$person = $serializer->deserialize($data,'Acme\Person','xml');

In your case it should be more like:

$person = $serializer->deserialize($data['data'],'Moodress\Bundle\PosteBundle\Entity\Poste','json');

Update:

Ok then.

First, your json file does not seem to be valid (use http://jsonlint.com/ to test it). Be careful of that.

Second, you will have to fetch your json as an array with

$data = json_decode($yourJsonFile, true);

and then you can access to each 'data' array with

foreach($data['data'] as $result)
{
    /* Here you can hydrate your object manually like:
    $person = new Person();
    $person->setId($user['id']);
    $person->setDescription($user['description']);

    Or you can use a denormalizer. */
}

I think the best solution here is to create new PosteResponse class, like this one:

namespace Moodress\Bundle\PosteBundle\Response;

use JMS\Serializer\Annotation\Type;

class PosteResponse
{
    /**
     * @Type("integer")
     */
    private $total;

    /**
     * @Type("array<Moodress\Bundle\PosteBundle\Entity\Poste>")
     */
    private $data;

    // Getters and setters here...
}

and deserialize your response to that class:

$response = $serializer->deserialize(
    $json,
    'Moodress\Bundle\PosteBundle\Response\PosteResponse',
    'json'
);
$posts = $response->getData();

That WILL do the trick, and it doesn't require you to decode and encode your json manually which is riddiculous in my opinion.

A less than ideal solution that I found was to first decode and then encode the json data again at the node that represents the data array. For example in your case:

$json = json_decode($json);
$json = json_encode($json->data);
$serializer->deserialize($json, 'array<Moodress\Bundle\PosteBundle\Entity\Poste>', 'json');

There must be a better solution than this but this seems more elegant than the above solution of de-serialising json.

Since Symfony Serializer Component 2.8 to deserialize array of objects:

$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');

https://symfony.com/doc/master/components/serializer.html#handling-arrays

I would make something like this

class PostsModel
{
    /**
     * @var int
     */
    private $total;

    /**
     * @var PostModel[]
     */
    private $data;
}

class PostModel
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var UserModel
     */
    private $user;

    /**
     * @var string
     */
    private $description;

    /**
     * @var  int
     */
    private $nb_comments;

    /**
     * @var int
     */
    private $nb_likes;

    /**
     * @var \DateTime
     */
    private $date_creation;
}

class UserModel
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $username;
}

And in controller

            $posts = $this->serializer->deserialize($data, PostsModel::class, 'json');

And this will return $postsModel with $data property which will have your array of entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!