错误使用:
$json_raw = '{"张三":"16","李四":"18"}';$res = json_decode($json_raw);$res[张三]; //把 json_decode() 后的对象当作数组使用。
正确使用(3种):
1、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true。2、json_decode($res) 返回的是一个对象, 不可以使用 $res['key'] 进行访问, 换成 $res->key 就可以了。<?php//第一种使用$json = '{"张三":"16","李四":"18","天龙":"200","转到":"20"}';$e = object_array(json_decode($json));//var_dump($e);echo $e[张三];function object_array($array){ //object(stdclass)转array数组的函数if(is_object($array)){$array = (array)$array;}if(is_array($array)){foreach($array as $key=>$value){$array[$key] = object_array($value);}}return $array;} //第二种使用$json = '{"张三":"16","李四":"18","天龙":"200","转到":"20"}';$json_object= json_decode($json)echo $json_object -> 天龙;//第三种使用$json_raw = '[{"张三":"16","李四":"18","天龙":"200","转到":"20"},{"张9":"16","李四":"18","天龙":"19","转到":"20"}]';$json_array = json_decode($json_raw, true);$rt =$json_array[0];var_dump($rt);echo $rt[李四];?>