json_decode到数组

我正在尝试将JSON字符串解码为数组,但出现以下错误。

致命错误:不能在第6行的C:\\ wamp \\ www \\ temp \\ asklaila.php中将stdClass类型的对象用作数组

这是代码:

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

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

#1楼

以防万一您使用的PHP少于5.2,则可以使用此资源。

http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/

http://mike.teczno.com/JSON/JSON.phps


#2楼

这也将其更改为数组:

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

#3楼

这是最新的贡献,但是使用(array)强制转换json_decode是有效的。
考虑以下:

$jsondata = '';
$arr = json_decode($jsondata, true);
foreach ($arr as $k=>$v){
    echo $v; // etc.
}

如果$jsondata曾经作为空字符串返回(按照我的经验,通常这样),则json_decode将返回NULL ,从而导致错误警告:第3行为foreach()提供了无效的参数 。 您可以添加if / then代码行或三元运算符,但是IMO将第二行更改为...

$arr = (array) json_decode($jsondata,true);

...除非您json_decodejson_decode数百万个大型数组,在这种情况下,如@ TCB13所指出的那样,性能可能会受到负面影响。


#4楼

在PHP中json_decode将json数据转换为与PHP相关的数组
例如: $php-array= json_decode($json-data, true); print_r($php-array); $php-array= json_decode($json-data, true); print_r($php-array);


#5楼

请尝试这个

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

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
echo "<pre>"; print_r($obj['Result']);
?>

#6楼

尝试这样:

$json_string = 'https://example.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj->Result);
foreach($obj->Result as $value){
  echo $value->id; //change accordingly
}

#7楼

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!

#8楼

根据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)
}

#9楼

根据文档 ,您需要指定是否要关联数组而不是json_decode中的对象,这是代码:

json_decode($jsondata, true);

#10楼

尝试这个

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

#11楼

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

json_decode($data); // Returns collections 

因此,如果要使用数组,则可以在json_decode函数中将第二个参数传递为'true'。


#12楼

我希望这能帮到您

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

}

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

发布了0 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/104243898