![](/img/trans.png)
[英]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.