对于这个json api响应,我能够用json_decode解析:
$string = '{"name": "Google",
"permalink": "google",
"homepage_url": "http://google.com",
"blog_url": "http://googleblog.blogspot.com",
"blog_feed_url": "http://googleblog.blogspot.com/feeds/posts/default?alt=rss",
"twitter_username": "google",
"category_code": "search",
"number_of_employees": 20000,
"founded_year": 1998,
"founded_month": 9,
"founded_day": 7,// bla bla.....}';
$obj=json_decode($string);
echo $obj->number_of_employees."<br>";// 20000
echo $obj->founded_year; //1998
我得到了上面的结果但是得到了空白的结果,下面是一个:
$string = '{"offices":
[{"description": "Google Headquarters",
"address1": "1600 Amphitheatre Parkway",
"address2": "",
"zip_code": "",
"city": "Mountain View",
"state_code": "CA",
"country_code": "USA",//blah blah }]//blah blah...}';
$obj=json_decode($string);
echo $obj->address1."<br>";// ""
echo $obj->city; //""
我知道address1又在另一个数组或循环中,但不知道如何检索它......任何想法?
答案 0 :(得分:3)
您将需要以下内容:
foreach($obj->offices as $office) {
echo $office->address1; // The first would be '1600 Amphitheatre Parkway'
}
要查看已解码的json的内容,请执行以下操作:
echo "<pre>";
print_r($obj);
echo "</pre>";
在你的情况下,这会给你:
stdClass Object
(
[offices] => Array
(
[0] => stdClass Object
(
[description] => Google Headquarters
[address1] => 1600 Amphitheatre Parkway
[address2] =>
[zip_code] =>
[city] => Mountain View
[state_code] => CA
[country_code] => USA
)
)
)