Cannot use object of type stdClass as array PHP JSON出错:Cannot use object of type stdClass as array解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Cannot use object of type stdClass as array PHP JSON出错:Cannot use object of type stdClass as array解决

  2021-03-19 我要评论
想了解PHP JSON出错:Cannot use object of type stdClass as array解决办法的相关内容吗,在本文为您仔细讲解Cannot use object of type stdClass as array的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:PHP,JSON,Cannot,use,object,of,type,stdClass,as,array,下面大家一起来学习吧。

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

复制代码 代码如下:

Cannot use object of type stdClass as array

产生原因:
复制代码 代码如下:

$res = json_decode($res);
$res['key']; //把 json_decode() 后的对象当作数组使用。

解决方法(2种):

1、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true。
2、json_decode($res) 返回的是一个对象, 不可以使用 $res['key'] 进行访问, 换成 $res->key 就可以了。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们