=波波日志 > PHP/apache/Perl > PHP“Cannot use object of type stdClass as array”=

[转]PHP“Cannot use object of type stdClass as array”

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

错误:
Cannot use object of type stdClass as array

产生原因:
+展开
-PHP
    $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 就可以了。


参考手册:json_decode

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

来源:http://home.phpchina.com/space.php?uid=68442&do=blog&id=48792
类别:PHP/apache/Perl 作者:转载 日期:2011-04-27 【评论:1】 
上一篇:PHP JSON使用教程 
下一篇:php隐形字符65279
 
    • jason

  • 日期:2011-6-5 19:55:13  IP:222.75.*.*
    谢谢分享!!!
    管理员回复(2011-6-5 20:53:40)
    O(∩_∩)O~

发表留言
  • *昵称:
  • 头像:
  • 电子邮件: [留下您的邮件,方便管理员回复您。]
  • 个人网站: *验证码:
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载或引用的作品侵犯了您的权利,请通知我们,我们会及时删除!
Powered by showbo,©2012,桂ICP备05005887号 京公网安备1101055090