出现“Cannot use object of type stdClass as array”

php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误

错误:
Cannot use object of type stdClass as array
在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190320160839538.png在这里插入图片描述
改成后:
在这里插入图片描述

产生原因:
r e s = j s o n d e c o d e ( res = json_decode( res=jsondecode(res);
$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)使jsondecodetrue2jsondecode(res) 返回的是一个对象, 不可以使用 $res[‘key’] 进行访问, 换成 $res->key 就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值