php json_decode 到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5164404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json_decode to array
提问by Harsha M V
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']);
?>
回答by Stephen
As per the documentation, you need to specify if you want an associative array instead of an object from json_decode, this would be the code:
根据文档,您需要指定是否需要关联数组而不是来自的对象json_decode,这将是代码:
json_decode($jsondata, true);
回答by diEcho
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);
回答by neokio
This is a late contribution, but there is a valid case for casting json_decodewith (array).
Consider the following:
这是一个迟到的贡献,但有一个有效的案例可以json_decode使用(array).
考虑以下:
$jsondata = '';
$arr = json_decode($jsondata, true);
foreach ($arr as $k=>$v){
echo $v; // etc.
}
If $jsondatais ever returned as an empty string (as in my experience it often is), json_decodewill return NULL, resulting in the error Warning: Invalid argument supplied 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 ...
如果$jsondata作为空字符串返回(根据我的经验,它经常是),json_decode将返回NULL,导致错误警告:第 3 行为 foreach() 提供的参数无效。您可以添加一行 if/then 代码或三元运算符,但 IMO 将第 2 行更改为 ...
$arr = (array) json_decode($jsondata,true);
... unless you are json_decodeing millions of large arrays at once, in which case as @TCB13 points out, performance could be negatively effected.
...除非您同时json_decode处理数百万个大型数组,在这种情况下,正如@TCB13 指出的那样,性能可能会受到负面影响。
回答by Anuj Pandey
Just in case you are working on php less then 5.2 you can use this resourse.
万一您使用的 php 版本低于 5.2,您可以使用此资源。
http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/
http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/
回答by Arosha De Silva
According to the PHP Documentationjson_decodefunction has a parameter named assocwhich convert the returned objects into associative arrays
根据PHP Documentationjson_decode函数有一个名为assoc的参数,它将返回的对象转换为关联数组
mixed json_decode ( string $json [, bool $assoc = FALSE ] )
Since assocparameter is FALSEby default, You have to set this value to TRUEin 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)
}
回答by coreyavis
This will also change it into an array:
这也会将其更改为数组:
<?php
print_r((array) json_decode($object));
?>
回答by Arjun Kariyadan
json_decodesupport second argument, when it set to TRUEit will return an Arrayinstead of stdClass Object. Check the Manualpage of json_decodefunction to see all the supported arguments and its details.
json_decode支持第二个参数,当它设置为TRUE它将返回一个Array而不是stdClass Object。检查函数的手册页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!
回答by Shanu Singh
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_decodefunction.
因此,如果想要一个数组,则可以在json_decode函数中将第二个参数作为“true”传递。
回答by TechyFlick
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)
结果:计算机系统(网络)
回答by Salman Mohammad
in PHP json_decode convert json data into PHP associated array
For Ex: $php-array= json_decode($json-data, true);
print_r($php-array);
在 PHP json_decode 中将 json 数据转换为 PHP 关联数组
例如:$php-array= json_decode($json-data, true);
print_r($php-array);

