duanmiexi2275
duanmiexi2275
2018-09-20 18:49
浏览 42
已采纳

Symfony JMS Serializer将jsons数组反序列化为实体

I have this string:

[{
    "box": [{
        "UID": "bid1106"
    }, {
        "UID": "bid565"
    }, {
        "UID": "bid1105"
    }]
}, {
    "topseller": [{
        "UID": "pid1816z1"
    }, {
        "UID": "pid8840z100z06"
    }, {
        "UID": "pid2942z01z18"
    }, {
        "UID": "pid5863z0"
    }]
}, {
    "global": {
        "templatename": "homepage",
        "locationid": "",
        "controlgroup": "false"
    }
}]

I need somehow to make Object using JMS serializer.

$obj = $this->serializer->deserialize($jsonData, ObjectClass::class, 'json');

And my ObjectClass.php class looks like this

class ObjectClass
{
    private $box = [];
    private $topseller = [];
    private $global = [];
    ....
}

Any suggestions, how I can make it as a single Object?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqybeh2884
    dqybeh2884 2018-09-20 19:13
    已采纳

    I just figure it out by my self:

        $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');
    
    点赞 评论

相关推荐