PHP json_decode object时报错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($data, true)。就是使json_decode 的第二个变量设置为 true。
2、json_decode($res) 返回的是一个对象, 不可以使用 $res['key'] 进行访问, 换成 $res->key 就可以了。

参考手册:json_decode
Return Values:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.

PHP json_decode object时报错Cannot use object of type stdClass as array的更多相关文章

  1. PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  2. PHP“Cannot use object of type stdClass as array”

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  3. iwebshop (: Cannot use object of type stdClass as array in)

    今天在PHP输出一个二维数组的时候,出现了“Fatal error: Cannot use object of type stdClass as array in……”. 这个二维数组是这样的: Ar ...

  4. **PHP错误Cannot use object of type stdClass as array in错误的

    错误:将PHP对象类型当做了PHP数组  解决方法:用对象操作符-> 今天在PHP输出一个二维数组的时候,出现了“Fatal error: Cannot use object of type s ...

  5. PyQt(Python+Qt)学习随笔:自定义信号在emit发射信号时报错:AttributeError: object has no attribute

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 如果使用自定义信号,一定要记得信号是类变量,必须在类中定义,不能在实例 ...

  6. facenet pyhton3.5 训练 train_softmax.py 时报错AttributeError: 'dict' object has no attribute 'iteritems'

    报错原因:在进行facenet进行train_softmax.py训练时,在一轮训练结束进行验证时,报错AttributeError: 'dict' object has no attribute ' ...

  7. 解决Linq Join Group by 时报错:Nullable object must have a value.

    Linq Join Group by 时报Nullable object must have a value. 例如: from s in subject on ch.SubId equals s.S ...

  8. peewee insert 数据时报错:'buffer' object has no attribute 'translate'

    错误信息: "'buffer' object has no attribute 'translate'" 场景:使用peewee insert 数据时,BlobField 字段存储 ...

  9. zc.buildout构建项目时报错‘AttributeError: '_NamespacePath' object has no attribute 'sort'’

    在使用zc.buildout构建项目时如果碰到‘AttributeError: '_NamespacePath' object has no attribute 'sort'’报错: An inter ...

随机推荐

  1. 关于js的call()和apply()两个函数的一点个人看法

    首先说明一下,call()和apply都是js的内置函数 它的作用是:改变call或者apply函数里面的``第一个参数对象``的指针,使它转向引用它的函数 call()的用法,call(对象,参数1 ...

  2. JQuery:JQuery添加元素

    JQuery:添加元素通过 jQuery,可以很容易地添加新元素/内容.添加新的HTML内容.我们将学习用于添加新内容的四个jQuery方法: append() - 在被选元素的结尾插入内容 prep ...

  3. android handler调用post方法阻塞

    1.试下用postDelayed(Runnable a, int time),因为post把消息放到Looper中就返回,但Looper中没有其他消息又会被立刻取出来执行,这样就有可能做了run中的操 ...

  4. Visual Studio 2015 + IIS Express 10.0 调试 ASP.NET 项目

    参考资料: https://msdn.microsoft.com/zh-cn/library/58wxa9w5(v=vs.120).aspx 首先搭建环境, 也就是用 IIS Express 配置一个 ...

  5. iOS 对UIButton的imageView和titleLabel进行重新布局

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  6. ORA-19693: 已包括备份片段

    使用rman异机恢复数据时,报"ORA-19693: 已包括备份片段..."错误. 执行的rman命令: RMAN> run{ 2> allocate channel ...

  7. 在xocde运行profile 遇到"Existing default base temp directory '/Library/Caches/com.apple.dt.instruments' has insufficient privileges for user id 505. Please have the owner delete this directory"

    找到这篇文章 http://stackoverflow.com/questions/34153795/xcode-7-unable-to-open-instruments-from-developer ...

  8. PostgreSQL9.2.4内核源码结构介绍

    PostgreSQL的源代码可以随意获得,其开源协议也允许研究者任意修改,这里介绍一下PostgreSQL的源码结构以及部分实现机制.下载PostgreSQL源代码并减压后,其一级目录结构如下图: P ...

  9. JavaScript内的类型转换

    JavaScript内的类型转换 1.分为自动转换和强制转换,我们一般用强制转换.其他类型转换为整数是parseInt();其他类型转化为小数parseFloat(); 2.判断是不是一个合法数字   ...

  10. java BigInteger

    用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂.用Java刷了一些题,感觉Java还不错,在处理高精度和进制转换中,调用库函数的来处理.下面是写的一些Java中 ...