Php json_解码到数组
我试图将JSON字符串解码为数组,但出现以下错误 致命错误:无法使用类型为的对象 中的stdClass作为数组 C:\wamp\www\temp\asklaila.php联机 六, 代码如下:Php json_解码到数组,php,arrays,json,Php,Arrays,Json,我试图将JSON字符串解码为数组,但出现以下错误 致命错误:无法使用类型为的对象 中的stdClass作为数组 C:\wamp\www\temp\asklaila.php联机 六, 代码如下: <?php $json_string = 'http://www.domain.com/jsondata.json'; $jsondata = file_get_contents($json_string); $obj = json_decode($jsondata); print_r($obj[
<?php
$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);
?>
$result = json_decode($jsondata, true);
根据,如果希望使用关联数组而不是json\u decode
中的对象,则需要指定true
作为第二个参数。这就是代码:
<?php
$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);
?>
$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);
$json\u string=>http://www.domain.com/jsondata.json';
$jsondata=file\u get\u contents($json\u string);
$obj=json_decode($jsondata,true);
回声“;
印刷费($obj);
如果您使用的php小于5.2,您可以使用此资源
这也会将其更改为一个数组:
$jsondata = '';
$arr = json_decode($jsondata, true);
foreach ($arr as $k=>$v){
echo $v; // etc.
}
这是一个迟来的贡献,但是使用
(数组)
考虑以下事项:
$arr = (array) json_decode($jsondata,true);
如果将$jsondata
作为空字符串返回(根据我的经验,通常是这样),json\u decode
将返回NULL
,导致错误警告:为第3行的foreach()提供的参数无效。您可以添加一行if/then代码或三元运算符,但在我看来,只需将第2行更改为
<?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\u解码
ing,在这种情况下,@TCB13指出,性能可能会受到负面影响。在PHP json\u解码中,将json数据转换为与PHP相关联的数组
例如:
$php array=json\u decode($json data,true);
打印(php数组)代码>请尝试此选项
$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
}
试着这样做:
json_decode($data, true); // Returns data in array format
json_decode($data); // Returns collections
json\u decode
支持第二个参数,当它设置为TRUE
时,它将返回一个数组
,而不是stdClass对象
。查看json_decode
函数的页面,查看所有支持的参数及其详细信息
例如,尝试以下方法:
$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\u decode
函数有一个名为assoc的参数,该参数将返回的对象转换为关联数组
混合json_解码(字符串$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));
哪些产出:
对象(stdClass)#1(5){
[“a”]=>int(1)
[“b”]=>int(2)
[“c”]=>int(3)
[“d”]=>int(4)
[“e”]=>int(5)
}
阵列(5){
[“a”]=>int(1)
[“b”]=>int(2)
[“c”]=>int(3)
[“d”]=>int(4)
[“e”]=>int(5)
}
因此,如果需要数组,可以在
json\u decode
函数中将第二个参数作为'true'传递。我希望这对您有所帮助
foreach($json_pss['courseList'] as $pss_json)
{
echo '<br>' .$course_data1 = $pss_json['course_data1']; exit;
}
使用Json解码功能
在php中的JSON数组上循环
foreach($json\u pss['courseList']作为$pss\u json)
{
回显“
”。$course\u data1=$pss\u json['course\u data1'];退出;
}
结果:计算机系统(网络)它提出了一个问题,让它作为数组而不是对象返回有什么好处?它提出了一个问题。“乞求一个问题”意味着假设一些有待证明的东西()。在这两种情况下,优点可能是OP比对象更容易遍历数组,或者其他一些已经实现的代码需要数组。@jamesnotjim返回对象的默认实现可能会提出这样一个问题:对象的返回值比数组的返回值更好,不是吗?确实可以@DavidMann。触摸!我想补充一点(尽管是几年后),即JSON不可能包含除数据以外的任何内容,这使得它成为一个令人困惑的“默认”选择。这是CPU/内存的浪费,正如建议的
JSON\u decode($object,true)
的true
功能完全相同,内部速度更快。@TCB13除非您同时需要这两个功能,并且不想使用@JimmyKane运行decode againcour。我试着在一个循环中运行这两个版本;如果您同时需要对象和数组(尽管这种情况应该很少发生?),那么json_decode
+强制转换比运行两种风格的json_decode
快45%。另一方面,两者的速度都非常快,除非你真的需要数千个编码,否则差别可以忽略不计。如果你用$ob->Result
来访问,它就会起作用。
foreach($json_pss['courseList'] as $pss_json)
{
echo '<br>' .$course_data1 = $pss_json['course_data1']; exit;
}