[英]json_decode to array
我正在尝试将 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']);
?>
根据文档,如果您想要一个关联数组而不是来自json_decode
的对象,则需要指定true
作为第二个参数。 这将是代码:
$result = json_decode($jsondata, true);
如果您想要integer
键而不是任何属性名称:
$result = array_values(json_decode($jsondata, true));
但是,使用您当前的解码,您只需将其作为对象访问:
print_r($obj->Result);
尝试这个
$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo "<pre>";
print_r($obj);
这是一个较晚的贡献,但有一个有效的案例可以使用(array)
转换json_decode
。
考虑以下:
$jsondata = '';
$arr = json_decode($jsondata, true);
foreach ($arr as $k=>$v){
echo $v; // etc.
}
如果$jsondata
曾经作为空字符串返回(根据我的经验,它经常是这样), json_decode
将返回NULL
,导致错误Warning: Invalid argument provided for foreach() on line 3 。 您可以添加一行 if/then 代码或三元运算符,但 IMO 只需将第 2 行更改为 ...
$arr = (array) json_decode($jsondata,true);
...除非您json_decode
处理数百万个大型数组,在这种情况下,正如@TCB13 指出的那样,性能可能会受到负面影响。
万一您正在使用低于 5.2 的 php,您可以使用此资源。
http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/
根据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)
}
这也将其更改为数组:
<?php
print_r((array) json_decode($object));
?>
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!
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;
}
结果:计算机系统(网络)
在 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);
请试试这个
<?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']);
?>
试试这样:
$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.