Symfony JMS Serializer 将 json 数组反序列化为一个实体
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.phpclass看起来像这样
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');
我有这个字符串:
[{
"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.phpclass看起来像这样
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');