使用json_decode()创建数组而不是对象。

489
我正在尝试将一个JSON字符串解码为数组,但是我遇到了以下错误。

致命错误:无法将stdClass对象用作数组

这里是代码:

$json_string = 'http://www.example.com/jsondata.json';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);

2
如果你使用 $ob->Result 访问,它就能正常工作。 - lapin
12个回答

971
根据文档,如果你想从json_decode得到一个关联数组而不是一个对象,需要将第二个参数指定为true。代码如下:

$result = json_decode($jsondata, true);

如果你想要使用 整数 作为键名而非属性名:

$result = array_values(json_decode($jsondata, true));

然而,使用您当前的解码方式,您只能将其视为对象进行访问:

print_r($obj->Result);

4
这引出一个问题,将其返回为数组而不是对象的优点是什么? - Foxinni
62
“它引发了一个问题”,“假设一个问题”意味着假设仍有待证明的某些事情。不管哪种情况,优势可能在于,原始发布者比起对象更习惯于遍历数组,或者是因为其他已实现的代码需要一个数组。 - jamesnotjim
8
默认实现返回一个对象可能会引发这样的问题:难道对象比数组更适合作为返回值吗? - David Mann
7
确实可以,@DavidMann。戳中要害了! - jamesnotjim
9
我想补充一下(虽然是几年后),JSON 没有包含除数据以外的任何东西的可能性,这使得它成为一个令人困惑的“默认”选择。 - Barry
显示剩余4条评论

49

试试这个

$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo "<pre>";
print_r($obj);

31

虽然有点晚了,但是将 json_decode 强制转换成 (array) 是有其合理的用例的。
请考虑以下内容:

$jsondata = '';
$arr = json_decode($jsondata, true);
foreach ($arr as $k=>$v){
    echo $v; // etc.
}
如果$jsondata返回为空字符串(在我遇到的情况中经常如此),json_decode将返回NULL,从而导致错误警告:无效参数在第3行提供foreach()。您可以添加一行if /then代码或三元运算符,但我认为更清洁的方法是将第2行更改为...
$arr = (array) json_decode($jsondata,true);

...除非您一次json_decode数百万个大数组,否则正如@TCB13所指出的那样,性能可能会受到负面影响。


6
根据PHP文档json_decode函数有一个名为assoc的参数,它可以将返回的对象转换成关联数组。
 mixed json_decode ( string $json [, bool $assoc = FALSE ] )

由于默认情况下,assoc参数为FALSE,因此您必须将该值设置为TRUE才能检索数组。

请查看以下代码以了解示例实现:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));

输出结果如下:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

6

5
这也会将它转换为数组:
<?php
    print_r((array) json_decode($object));
?>

6
建议使用 json_decode($object, true); 中的 true 参数,它会在内部执行相同的操作,但更加快速,避免浪费CPU/内存资源。 - TCB13
1
@TCB13,除非您需要两者并且不想再次运行解码。 - Jimmy Kane
3
赞同@JimmyKane的观点。我尝试在循环中运行两个版本;如果你需要对象和数组(虽然这应该很少发生?),json_decode+转换比运行两种类型的json_decode要快45%。另一方面,两种方法都非常快,除非你真的需要进行成千上万次的解码,否则差异是可以忽略不计的。 - LSerni

5
json_decode($data, true); // Returns data in array format 

json_decode($data); // Returns collections 

所以,如果想要一个数组,可以在 json_decode 函数中传递第二个参数为'true'。


4

json_decode支持第二个参数,当设置为TRUE时,它将返回一个Array而不是stdClass Object。请查看json_decode函数的手册页面以了解所有支持的参数及其详细信息。

例如,请尝试以下操作:

$json_string = 'http://www.example.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, TRUE); // Set second argument as TRUE
print_r($obj['Result']); // Now this will works!

4

希望这能对您有所帮助

$json_ps = '{"courseList":[  
            {"course":"1", "course_data1":"Computer Systems(Networks)"},  
            {"course":"2", "course_data2":"Audio and Music Technology"},  
            {"course":"3", "course_data3":"MBA Digital Marketing"}  
        ]}';

请使用Json解码函数。
$json_pss = json_decode($json_ps, true);

在php中遍历JSON数组

foreach($json_pss['courseList'] as $pss_json)
{

    echo '<br>' .$course_data1 = $pss_json['course_data1']; exit; 

}

结果:计算机系统(网络)


2

在PHP中,json_decode可以将JSON数据转换为PHP关联数组。
例如:$php-array= json_decode($json-data, true); print_r($php-array);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接