Jar*_*wol 5 serialization symfony php-7
序列化和反序列化单个实体对象适合我.
以这种方式可以序列化和反序列化多个对象(对象数组)?
$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);
Run Code Online (Sandbox Code Playgroud)
和
$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');
Run Code Online (Sandbox Code Playgroud)
然后我得到了:
属性路径构造函数需要字符串或"Symfony\Component\PropertyAccess\PropertyPath"的实例.得到:"整数"500内部服务器错误 - 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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3317 次 |
| 最近记录: |