json对象的变换,新手求大神帮忙
时间:2021-07-01 10:21:17
帮助过:41人阅读
json对象的转换,新手求大神帮忙
json格式如下
{"loginResult":{"code":"005","teamId":"1","password":"123","role":"员工","roleId":"2"}}
我用的CI框架 在models 里面定义了一个Member_Model对象,属性有code,teamId,password,role,roleId
怎样将上面的JSON数据转换成对象赋给Member_Model
------解决方案--------------------$json = '{"loginResult":{"code":"005","teamId":"1","password":"123","role":"员工","roleId":"2"}}';
$arr = json_decode($json);
$mm = new Member_Model();
foreach ($arr->loginResult as $key => $value) {
$mm->$key = $value;
}
------解决方案--------------------$s='{"loginResult":{"code":"005","teamId":"1","password":"123","role":"员工","roleId":"2"}}';
$o = json_decode($s);
print_r($o->loginResult);
------解决方案--------------------如果是
$result = json_decode($buffer, true);
则
$member = $result['loginResult'];
如果是
$result = json_decode($buffer);
则
$member = $result->loginResult;