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?

图片转代码服务由CSDN问答提供 功能建议

我有这个字符串:

  [{
 n“ 框“:[{
”UID“:”bid1106“
},{
”UID“:”bid565“
},{
”UID“:”bid1105“
}] 
},  {
“topseller”:[{
“UID”:“pid1816z1”
},{
“UID”:“pid8840z100z06”
},{
“UID”:“pid2942z01z18”
},  {
“UID”:“pid5863z0”
}] 
},{
“global”:{
“templatename”:“homepage”,
“locationid”:“”,
“controlgroup”  :“false”
} 
}] 
   
 
 

我需要以某种方式使用JMS序列化器创建Object。

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

和我的ObjectClass。 php类看起来像这样

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

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

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

1条回答 默认 最新

  • 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');
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题