Home > Backend Development > PHP Tutorial > PHP JSON出错:Cannot use object of type stdClass as array解决方法_PHP

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

WBOY
Release: 2016-05-31 19:30:50
Original
1304 people have browsed it

JSON

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 就可以了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template