我试图解码JSON字符串到一个数组,但我得到以下错误。

致命错误:不能使用类型的对象 作为数组中的stdClass C:\wamp\www\temp\asklaila.php联机 6

代码如下:

<?php
$json_string = 'http://www.domain.com/jsondata.json';

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

当前回答

在PHP json_decode转换json数据到PHP相关数组 $php-array= json_decode($json-data, true); print_r ($ php数组);

其他回答

这也会把它变成一个数组:

<?php
    print_r((array) json_decode($object));
?>

根据文档,如果您想要一个关联数组而不是来自json_decode的对象,则需要将true指定为第二个参数。代码如下:

$result = json_decode($jsondata, true);

如果你想要整型键而不是属性名:

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

然而,在你当前的解码中,你只是将它作为一个对象访问:

print_r($obj->Result);

在PHP json_decode转换json数据到PHP相关数组 $php-array= json_decode($json-data, true); print_r ($ php数组);

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

json_decode($data); // Returns collections 

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

我希望这对你有所帮助

$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; 

}

结果:电脑系统(网络)