gpt4 book ai didi

serialization - Symfony3 反序列化多个实体对象

转载 作者:行者123 更新时间:2023-12-04 18:59:38 28 4
gpt4 key购买 nike

序列化和反序列化单个实体对象对我来说正常工作。

可以以这种方式序列化和反序列化多个对象(对象数组)??

$notifications = $this->getDoctrine()
->getRepository('AppBundle:Notification')
->findAll();

$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();

$serializer = new Serializer(array($normalizer), array($encoder));
$jsonContent = $serializer->serialize($notifications, 'json');

return new Response($jsonContent);


$response = curl_exec($ch); // my $jsonContent from previous code

$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();

$serializer = new Serializer(array($normalizer), array($encoder));
$notifications = $serializer->deserialize($response, Notification::class, 'json');

然后我得到:

The property path constructor needs a string or an instance of "Symfony\Component\PropertyAccess\PropertyPath". Got: "integer" 500 Internal Server Error - UnexpectedValueException

最佳答案

我找到了解决方案

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;

$serializer = new Serializer(
array(new GetSetMethodNormalizer(), new ArrayDenormalizer()),
array(new JsonEncoder())
);

$data = ...; // The serialized data from the previous example
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');

关于serialization - Symfony3 反序列化多个实体对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40963905/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com