php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误
错误:
Cannot use object of type stdClass as array
;
$res[‘key’]; //把 json_decode() 后的对象当作数组使用。
解决方法(2种):
1、使用 json_decode(
d
,
t
r
u
e
)
。
就
是
使
j
s
o
n
d
e
c
o
d
e
的
第
二
个
变
量
设
置
为
t
r
u
e
。
2
、
j
s
o
n
d
e
c
o
d
e
(
d, true)。就是使json_decode 的第二个变量设置为 true。 2、json_decode(
d,true)。就是使jsondecode的第二个变量设置为true。2、jsondecode(res) 返回的是一个对象, 不可以使用 $res[‘key’] 进行访问, 换成 $res->key 就可以了。