json_decode 到数组 [英] json_decode to array
问题描述
我正在尝试将 JSON 字符串解码为数组,但出现以下错误.
I am trying to decode a JSON string into an array but i get the following error.
致命错误:无法使用类型的对象stdClass 作为数组C:\wamp\www\temp\asklaila.php 上线6
Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\temp\asklaila.php on line 6
代码如下:
<?php
$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);
?>
推荐答案
根据文档,您需要如果您想要关联数组而不是 json_decode
中的对象,请指定 true
作为第二个参数.这将是代码:
As per the documentation, you need to specify true
as the second argument if you want an associative array instead of an object from json_decode
. This would be the code:
$result = json_decode($jsondata, true);
如果你想要 integer
键而不是任何属性名称:
If you want integer
keys instead of whatever the property names are:
$result = array_values(json_decode($jsondata, true));
但是,使用您当前的解码,您只需将其作为对象访问:
However, with your current decode you just access it as an object:
print_r($obj->Result);
这篇关于json_decode 到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!