JSON

encode “Cannot use object of type stdClass as array” 的解

字号+ 作者:H5之家 来源:H5之家 2015-10-06 13:29 我要评论( )

错误: Cannot use object of type stdClass as array 产生原因: $res = json_decode($res); $res[

你的位置:飞飞的个人网站 > 资讯 > php json_encode “Cannot use object of type stdClass as array” 的解决方法

php json_encode “Cannot use object of type stdClass as array” 的解决方法

资讯 feifei 3年前 (2012-11-01) 517浏览

错误:

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

参考手册:json_decode

Return Values:Returns anobjector if the optionalassocparameter isTRUE, an associativearrayis instead returned.

转载请注明:飞飞的个人网站 » php json_encode “Cannot use object of type stdClass as array” 的解决方法

分享到

上一篇 网站响应头http错误代码表 天敏的电视盒黑屏情况下调分辨率的方法 下一篇

与本文相关的文章

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Objective-C与JavaScript交互的那些事

    Objective-C与JavaScript交互的那些事

    2016-01-30 13:00

  • json_encode转码

    json_encode转码

    2016-01-17 12:52

  • PHP中json_encode 支持中文解决方法

    PHP中json_encode 支持中文解决方法

    2016-01-15 19:23

  • PHP中json_encode函数不转义中文

    PHP中json_encode函数不转义中文

    2016-01-15 15:14

网友点评
e