Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP json_解码显示信息_Php_Json_Decode - Fatal编程技术网

使用PHP json_解码显示信息

使用PHP json_解码显示信息,php,json,decode,Php,Json,Decode,我试图通过json_解码从json返回的输出中提取不同的信息,但是我似乎无法访问这些信息: JSON: PHP: $photos=json\u decode($json); foreach($photos作为$photo){ $id=$photo->id; $owner=$photo->owner; $secret=$photo->secret; 回显$id.“”; echo$owner.“”; echo$secret.“”; } 由于JSON的深度,您必须在正确的级别开始循环。以下是一份: $

我试图通过json_解码从json返回的输出中提取不同的信息,但是我似乎无法访问这些信息:

JSON:

PHP:

$photos=json\u decode($json);
foreach($photos作为$photo){
$id=$photo->id;
$owner=$photo->owner;
$secret=$photo->secret;
回显$id.“
”; echo$owner.“
”; echo$secret.“
”; }
由于JSON的深度,您必须在正确的级别开始循环。以下是一份:

$json='{“照片”:{“页面”:1,“页面”:8,“每页”:100,“总计”:“784”,“照片”:[
{“id”:“3456456”,“所有者”:1111111111@N03“,”机密“:”xxxxxxxx“,”服务器“:”4544“,”农场“:5,“标题“:”XXXXXXX“,”iPublic“:1,“iFriend”:0,“iFamily”:0},
{“id”:“5468564564”,“所有者”:1111111111@N03“,”机密“:”xxxxxxxx“,”服务器“:”4529“,”农场“:5,“标题“:”XXXXXXX“,”ispublic“:1,“isfriend”:0,“isfamily”:0}]}”;
$photos=json_decode($json);
foreach($photos->photos->photoas$photo){
$id=$photo->id;
$owner=$photo->owner;
$secret=$photo->secret;
回显$id.“
”; echo$owner.“
”; echo$secret.“
”; }

请注意,JSON中的第一级是“照片”,第二级是“照片”。

似乎您必须执行
foreach($photos->photo as$photo)
。如果您非常漂亮地打印json(即使用jsonlint.com),那么数据结构实际上应该非常清晰,只是稍微深入一点
foreach($photos->photos->photoas$photo)
json对象的奇怪设置。几乎就像是在“照片”下先填充所有内容,而这本应该是单数“照片”下的一系列内容(奇怪的是,这指的是很多)。
{"photos":{"page":1,"pages":8,"perpage":100,"total":"784","photo":[
{"id":"3453456456","owner":"1111111111@N03","secret":"xxxxxxxx","server":"4544","farm":5,"title":"XXXXXXX","ispublic":1,"isfriend":0,"isfamily":0},
{"id":"5468564564","owner":"1111111111@N03","secret":"xxxxxxxx","server":"4529","farm":5,"title":"XXXXXXX","ispublic":1,"isfriend":0,"isfamily":0},
$photos = json_decode($json);
foreach($photos as $photo){
$id = $photo->id;
$owner = $photo->owner;
$secret = $photo->secret;
echo $id.'<br/>';
echo $owner.'<br/>';
echo $secret.'<br/>';
}
$json = '{"photos":{"page":1,"pages":8,"perpage":100,"total":"784","photo":[
{"id":"3453456456","owner":"1111111111@N03","secret":"xxxxxxxx","server":"4544","farm":5,"title":"XXXXXXX","ispublic":1,"isfriend":0,"isfamily":0},
{"id":"5468564564","owner":"1111111111@N03","secret":"xxxxxxxx","server":"4529","farm":5,"title":"XXXXXXX","ispublic":1,"isfriend":0,"isfamily":0}]}}';

$photos = json_decode($json);

foreach($photos->photos->photo as $photo){
 $id = $photo->id;
 $owner = $photo->owner;
 $secret = $photo->secret;
 echo $id.'<br/>';
 echo $owner.'<br/>';
 echo $secret.'<br/>';
}