简体   繁体   English

json_decode 到数组

[英]json_decode to array

I am trying to decode a JSON string into an array but i get the following error.我正在尝试将 JSON 字符串解码为数组,但出现以下错误。

Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\temp\asklaila.php on line 6致命错误:不能在第 6 行的 C:\wamp\www\temp\asklaila.php 中使用 stdClass 类型的对象作为数组

Here is the code:这是代码:

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

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

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 .根据文档,如果您想要一个关联数组而不是来自json_decode的对象,则需要指定true作为第二个参数。 This would be the code:这将是代码:

$result = json_decode($jsondata, true);

If you want integer keys instead of whatever the property names are:如果您想要integer键而不是任何属性名称:

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

However, with your current decode you just access it as an object:但是,使用您当前的解码,您只需将其作为对象访问:

print_r($obj->Result);

try this尝试这个

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

This is a late contribution, but there is a valid case for casting json_decode with (array) .这是一个较晚的贡献,但有一个有效的案例可以使用(array)转换json_decode
Consider the following:考虑以下:

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

If $jsondata is ever returned as an empty string (as in my experience it often is), json_decode will return NULL , resulting in the error Warning: Invalid argument supplied for foreach() on line 3 .如果$jsondata曾经作为空字符串返回(根据我的经验,它经常是这样), json_decode将返回NULL ,导致错误Warning: Invalid argument provided for foreach() on line 3 You could add a line of if/then code or a ternary operator, but IMO it's cleaner to simply change line 2 to ...您可以添加一行 if/then 代码或三元运算符,但 IMO 只需将第 2 行更改为 ...

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

... unless you are json_decode ing millions of large arrays at once, in which case as @TCB13 points out, performance could be negatively effected. ...除非您json_decode处理数百万个大型数组,在这种情况下,正如@TCB13 指出的那样,性能可能会受到负面影响。

According to the PHP Documentation json_decode function has a parameter named assoc which convert the returned objects into associative arrays根据PHP 文档json_decode函数有一个名为assoc的参数,它将返回的对象转换为关联数组

 mixed json_decode ( string $json [, bool $assoc = FALSE ] )

Since assoc parameter is FALSE by default, You have to set this value to TRUE in order to retrieve an array.由于assoc参数默认为FALSE ,因此您必须将此值设置为TRUE才能检索数组。

Examine the below code for an example implication:检查以下代码以获取示例含义:

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

which outputs:输出:

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

This will also change it into an array:这也将其更改为数组:

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

json_decode support second argument, when it set to TRUE it will return an Array instead of stdClass Object . json_decode支持第二个参数,当它设置为TRUE时,它将返回一个Array而不是stdClass Object Check the Manual page of json_decode function to see all the supported arguments and its details.检查json_decode函数的手册页以查看所有支持的参数及其详细信息。

For example try this:例如试试这个:

$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!
json_decode($data, true); // Returns data in array format 

json_decode($data); // Returns collections 

So, If want an array than you can pass the second argument as 'true' in json_decode function.因此,如果想要一个数组,则可以在json_decode函数中将第二个参数作为“true”传递。

I hope this will help you我希望这能帮到您

$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"}  
        ]}';

Use Json decode function使用 Json 解码功能

$json_pss = json_decode($json_ps, true);

Looping over JSON array in php在php中循环JSON数组

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

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

}

Result : Computer Systems(Networks)结果:计算机系统(网络)

in PHP json_decode convert json data into PHP associated array在 PHP json_decode 中将 json 数据转换为 PHP 关联数组
For Ex: $php-array= json_decode($json-data, true); print_r($php-array);例如: $php-array= json_decode($json-data, true); print_r($php-array); $php-array= json_decode($json-data, true); print_r($php-array);

Please try this请试试这个

<?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']);
?>

Try like this:试试这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM