php Symfony - 将 json 反序列化为实体数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23051554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony - Deserialize json to an array of entities
提问by manonthemoon
I have a json object that I received by making a get API call. I make this call to receive a list of objects. It's a list of post... So I have an array of Post Objects.
我有一个通过 get API 调用收到的 json 对象。我进行此调用以接收对象列表。这是一个帖子列表......所以我有一个帖子对象数组。
Here the output :
这里的输出:
{
"total":2,
"data":[
{
"id":2,
"user":{
"id":1,
"username":"sandro.tchikovani"
},
"description":"cool",
"nb_comments":0,
"nb_likes":0,
"date_creation":"2014-04-13T20:07:34-0700"
},
{
"id":1,
"user":{
"id":1,
"username":"sandro.tchikovani",
},
"description":"Premier pooooste #lol",
"nb_comments":0,
"nb_likes":0,
"date_creation":"2014-04-13T15:15:35-0700"
}
]
}
I would like to deserialize the data part... The problem is that the Serializer in Symfony gives me an error ...
我想反序列化数据部分......问题是Symfony中的Serializer给了我一个错误......
The error that I have :
我的错误:
Class array<Moodress\Bundle\PosteBundle\Entity\Poste> does not exist
How I do deserialize :
我如何反序列化:
$lastPosts = $serializer->deserialize($data['data'], 'array<Moodress\Bundle\PosteBundle\Entity\Poste>', 'json');
How can I deserialze the data array... To have an array of Postes. I want to give to my view .twig an array Poste... I did precise the type when I deserialize... So I can't find what is the problem...
我怎样才能反序列化数据数组...要有一个 Postes 数组。我想给我的观点 .twig 一个数组 Poste ......我在反序列化时确实精确了类型......所以我找不到问题所在......
Thanks.
谢谢。
采纳答案by Einenlum
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.
首先,您的 json 文件似乎无效(使用http://jsonlint.com/ 对其进行测试)。小心点。
Second, you will have to fetch your json as an array with
其次,您必须将 json 作为数组获取
$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. */
}
回答by Mikz
I think the best solution here is to create new PosteResponse class, like this one:
我认为这里最好的解决方案是创建新的 PosteResponse 类,如下所示:
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.
这将解决问题,并且不需要您手动解码和编码您的 json,这在我看来是荒谬的。
回答by Malachi
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 = 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.
必须有比这更好的解决方案,但这似乎比反序列化 json 的上述解决方案更优雅。
回答by Einenlum
Since Symfony Serializer Component 2.8
to deserialize array of objects:
由于Symfony Serializer Component 2.8
反序列化对象数组:
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
https://symfony.com/doc/master/components/serializer.html#handling-arrays
https://symfony.com/doc/master/components/serializer.html#handling-arrays
回答by Ruslan Kovalov
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
这将返回带有 $data 属性的 $postsModel ,该属性将包含您的实体数组