[英]How to deserialize an array of complex objects with Symfony Serializer?
[英]Symfony JMS Serializer deserialize array of jsons to an entitie
我有这个字符串:
[{
"box": [{
"UID": "bid1106"
}, {
"UID": "bid565"
}, {
"UID": "bid1105"
}]
}, {
"topseller": [{
"UID": "pid1816z1"
}, {
"UID": "pid8840z100z06"
}, {
"UID": "pid2942z01z18"
}, {
"UID": "pid5863z0"
}]
}, {
"global": {
"templatename": "homepage",
"locationid": "",
"controlgroup": "false"
}
}]
我需要某种方式使用JMS序列化程序来制作对象。
$obj = $this->serializer->deserialize($jsonData, ObjectClass::class, 'json');
我的ObjectClass.php类看起来像这样
class ObjectClass
{
private $box = [];
private $topseller = [];
private $global = [];
....
}
有什么建议,如何使它成为单个对象?
我只是自己弄清楚:
$newArray =[];
foreach (json_decode($jsonData, true) as $item) {
$key = array_keys($item)[0];
$newArray[$key] = $item[$key];
}
$obj = $this->serializer->deserialize(json_encode($newArray), ObjectClass::class, 'json');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.