我完全不知道json。如何用php解析这个json
我需要解析返回这样的结果的JSON:
"response":{
"status":1,
"data":{
"16":{
"Offer":{
"id":"16",
"advertiser_id":null,
"name":"My Offer",
"description":null,
"require_approval":"0",
"require_terms_and_conditions":0,
"terms_and_conditions":null,
"offer_url":"http://",
"preview_url":"http://",
}
},
"17":{
"Offer":{
"id":"17",
"advertiser_id":null,
"name":"My Other Offer",
"description":null,
"require_approval":"0",
"require_terms_and_conditions":0,
"terms_and_conditions":null,
"offer_url":"http://",
"preview_url":"http://",
}
},
},
"errors":[]
}
我需要访问所有提供的名字,但我不能够访问尝试此:
$url = "jsonurl";
$string = file_get_contents($url);
$json_a=json_decode($string,true);
foreach ($json_a['data']['offer'] as $offer_a) {
echo $offer_a[name];
}
我做错了什么?
谢谢大家提前。
什么'的var_dump($ json_a);'说明了什么? – DaveRandom 2012-01-31 12:29:47
在foreach之前,添加var_dump($ json_a);并查看解码数据结构的外观。也许把它粘贴在这里,让代码获取你想要的数据会容易得多。 – ralfe 2012-01-31 12:30:42
你只显示一个摘录,但我认为你的实际结构更[像这样](http://array.include-once.org/?foreach=1&assoc=1&json=%7B%0D%0A%22response%22 %3A%7B%0D%0A%22data%22%3A%7B%0D%0A ++%2216%22%3A%7B%0D%0A ++++%22Offer%22%3A%7B%0D%0A ++++++%22id%22%3A %2216%22%2C%0D%0A ++++++%22name%22%3A%22My +优惠%22%2C%0D%0A ++++++%22description%22%3Anull%0D%0A ++++%7D%0D%0A ++%7D%0D%0A %7D%0D 0A%%7D%0D 0A%%7D)。所以请注意,您还需要'[“response”]'for foreach。 – mario 2012-01-31 12:37:48