json_decode到数组
我想解码一个JSONstring到一个数组,但我得到以下错误。
致命错误:不能使用stdClasstypes的对象作为第6行的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['Result']); ?>  根据文档 ,你需要指定如果你想要一个关联数组而不是来自json_decode的对象,这将是代码: 
 json_decode($jsondata, true); 
尝试这个
 $json_string = 'https://example.com/jsondata.json'; $jsondata = file_get_contents($json_string); $obj = json_decode($jsondata,true); echo "<pre>"; print_r($obj); 
 这是一个晚期的贡献,但有一个有效的情况下投掷json_decode (array) 。 
 考虑以下: 
 $jsondata = ''; $arr = json_decode($jsondata, true); foreach ($arr as $k=>$v){ echo $v; // etc. } 
 如果$jsondata被返回为空string(就像我经历的那样), json_decode将返回NULL ,导致错误警告:为第3行的foreach()提供的无效参数 。 你可以添加一行如果/然后代码或三元操作符,但国际海事组织是干净的只是简单地将第2行更改为… 
 $arr = (array) json_decode($jsondata,true); 
  …除非你是json_decode数以百万计的大型数组,在@ TCB13指出,性能可能会受到负面影响。 
以防万一你正在使用PHPless于5.2,你可以使用这个资源。
http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/
这也将它改变成一个数组:
 <?php print_r((array) json_decode($object)); ?> 
  json_decode支持第二个参数,当它设置为TRUE ,它将返回一个Array而不是stdClass Object 。 检查json_decode函数的Manual页面,查看所有支持的参数及其细节。 
例如试试这个:
 $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! 
请试试这个
 <?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']); ?> 
 在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); 
尝试像这样:
 $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 }