{ "query" : { "count" : 2,
"created" : "2014-03-08T10:16:51Z",
"lang" : "en-US",
"results" : { "place" : [ { "admin1" : { "code" : "IN-DL",
"content" : "Delhi",
"type" : "Territory",
"woeid" : "20070458"
},
"admin2" : { "code" : "",
"content" : "New Delhi",
"type" : "District",
"woeid" : "28743736"
},
"admin3" : null,
"areaRank" : "4",
"boundingBox" : { "northEast" : { "latitude" : "28.645321",
"longitude" : "77.240784"
},
"southWest" : { "latitude" : "28.569349",
"longitude" : "77.166237"
}
},
"centroid" : { "latitude" : "28.607330",
"longitude" : "77.203506"
},
"country" : { "code" : "IN",
"content" : "India",
"type" : "Country",
"woeid" : "23424848"
},
"lang" : "en-US",
"locality1" : { "content" : "Delhi",
"type" : "Town",
"woeid" : "2295019"
},
"locality2" : null,
"name" : "New Delhi",
"placeTypeName" : { "code" : "9",
"content" : "District"
},
"popRank" : "0",
"postal" : null,
"timezone" : { "content" : "Asia/Kolkata",
"type" : "Time Zone",
"woeid" : "28350818"
},
"uri" : "http://where.yahooapis.com/v1/place/28743736",
"woeid" : "28743736"
},
{ "admin1" : { "code" : "US-IL",
"content" : "Illinois",
"type" : "State",
"woeid" : "2347572"
},
"admin2" : { "code" : "",
"content" : "Jersey",
"type" : "County",
"woeid" : "12588119"
},
"admin3" : null,
"areaRank" : "2",
"boundingBox" : { "northEast" : { "latitude" : "39.043468",
"longitude" : "-90.245506"
},
"southWest" : { "latitude" : "39.025280",
"longitude" : "-90.268921"
}
},
"centroid" : { "latitude" : "39.034382",
"longitude" : "-90.257217"
},
"country" : { "code" : "US",
"content" : "United States",
"type" : "Country",
"woeid" : "23424977"
},
"lang" : "en-US",
"locality1" : { "content" : "New Delhi",
"type" : "Town",
"woeid" : "23510871"
},
"locality2" : null,
"name" : "New Delhi",
"placeTypeName" : { "code" : "7",
"content" : "Town"
},
"popRank" : "1",
"postal" : { "content" : "62052",
"type" : "Zip Code",
"woeid" : "12784947"
},
"timezone" : { "content" : "America/Chicago",
"type" : "Time Zone",
"woeid" : "56043661"
},
"uri" : "http://where.yahooapis.com/v1/place/23510871",
"woeid" : "23510871"
}
] }
} }
如何通过php获取质心,地点,国家等?
$json = curl_exec($ch); //RESULT of YQL QUERY
for($x=0;$x<1;$x++)
{
$lat = $json->results->place[$x]->centroid->latitude;
$lng = $json->results->place[$x]->centroid->longitude;
}
echo $lat;
echo $lng;
请帮忙
尝试此代码时:
$data = curl_exec($ch);
$json = json_decode($data, true);
// print_r($json);
if(is_array($json))
{
foreach($json as $query){
foreach($query['results']['place'] as $places){
if( is_array($places['centroid']) ){
echo "Centroid:\n";
echo "Latitude: " . $places['centroid']['latitude'] . "\n";
echo "longitude: " . $places['centroid']['longitude'] . "\n\n";
}
if( is_array($places['country']) ){
echo "Country:\n";
echo "Content: " . $places['country']['content'] . "\n";
echo "Woeid: " . $places['country']['woeid'] . "\n\n";
}
}
}
}
它不会进入if(is_array($json)) {
循环。
答案 0 :(得分:1)
请考虑此示例http://codepad.org/KbcRcAZd
试试这样:
$json = json_decode($data, true);
//print_r($json);
foreach($json as $query){
foreach($query['results']['place'] as $places){
echo "Latitude: " . $places['centroid']['latitude'] . "\n";
echo "longitude: " . $places['centroid']['longitude'] . "\n\n";
}
}
答案 1 :(得分:1)
这是通过将json设为数组然后显示
来实现此目的的另一种方法$data = json_decode(curl_exec($ch),true);
//print_r($data);
foreach($data["query"]["results"]["place"] as $key=>$val){
if(array_key_exists("centroid",$val)){
echo "-----------------------<br />";
foreach($val["centroid"] as $k=>$v){
echo $k.' :: '.$v;
echo '<br />';
}
echo "-----------------------<br />";
}
}
您可以签入print_r()数据的方式,并可以根据需要显示任何项目。
答案 2 :(得分:0)
您需要使用json_decode()
:
$json = json_decode(curl_exec($ch)); //RESULT of YQL QUERY
然后使用var_dump()
,您就可以查看对象中的内容并选择所需内容:
var_dump($json->results);