出现“Cannot use object of type stdClass as array”解决办法

àì夳堔傛蜴生んèń 2022-12-27 09:30 240阅读 0赞

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

一、错误位置

报错如图所示:
在这里插入图片描述
报错代码
在这里插入图片描述

二、解决办法

1、使用json_decode($res,true ) 。就是使jsond_ecode的第二个变量设置为true 。

  1. $data = json_decode($data,true);

2、json_decode($res) 返回的是一个对象, 不可以使用 $res[‘key’] 进行访问, 换成 $res->key 就可以了。

三、原因
  1. $res = json_decode($res);
  2. $res['key']; //把 json_decode() 后的对象当作数组使用。

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读