Symfony JMS序列化器将json数组反序列化为一个整数

时间:2018-09-20 18:49:38

标签: php symfony deserialization json-deserialization jmsserializerbundle

我有这个字符串:

[{
    "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 = [];
    ....
}

任何建议,如何将其作为单个对象?

1 个答案:

答案 0 :(得分:0)

我只是自己弄清楚:

    $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');