Home >Backend Development >PHP Tutorial >json对象的变换,新手求大神帮忙

json对象的变换,新手求大神帮忙

WBOY
WBOYOriginal
2016-06-13 11:51:26793browse

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"}}';<br />$arr = json_decode($json);<br />$mm = new Member_Model();<br />foreach ($arr->loginResult as $key => $value) {<br />	$mm->$key = $value;<br />}

------解决方案--------------------
$s='{"loginResult":{"code":"005","teamId":"1","password":"123","role":"员工","roleId":"2"}}';<br />$o = json_decode($s);<br />print_r($o->loginResult);

------解决方案--------------------
如果是
$result = json_decode($buffer, true);

$member = $result['loginResult'];

如果是
$result = json_decode($buffer);

$member = $result->loginResult;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn