我有这个字符串:
[{
"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');