我试图通过json_decode从json返回的输出中提取不同的信息,但我似乎无法访问该信息:
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},
PHP:
$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/>';
}
答案 0 :(得分:2)
由于JSON的深度,您必须在正确的级别启动循环。这是一个example:
$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/>';
}
请注意,您的JSON中的第一个级别是&#34;照片&#34;第二个是&#34;照片&#34;。