解析Json Google GeoMaps

时间:2017-01-12 10:15:42

标签: json

我正在尝试解析谷歌地图的响应,这是JSON响应。

{ "results" : [ { "address_components" : [ { "long_name" : "11", "short_name" : "11", "types" : [ "street_number" ] }, { "long_name" : "Shottermill", "short_name" : "Shottermill", "types" : [ "route" ] }, { "long_name" : "Horsham", "short_name" : "Horsham", "types" : [ "postal_town" ] }, { "long_name" : "West Sussex", "short_name" : "West Sussex", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "England", "short_name" : "England", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United Kingdom", "short_name" : "GB", "types" : [ "country", "political" ] }, { "long_name" : "RH12 5HG", "short_name" : "RH12 5HG", "types" : [ "postal_code" ] } ], "formatted_address" : "11 Shottermill, Horsham RH12 5HG, UK", "geometry" : { "location" : { "lat" : 51.084119, "lng" : -0.2948757 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 51.0854679802915, "lng" : -0.2935267197084979 }, "southwest" : { "lat" : 51.0827700197085, "lng" : -0.296224680291502 } } }, "place_id" : "ChIJ0ZUiT-_rdUgRadZ4jXXzam4", "types" : [ "street_address" ] } ], "status" : "OK" }

如果我想例如回显几何(lat和lng),我该怎么做。

我试过:( $ content包含上述回复)

$json = json_decode($content);
echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;

1 个答案:

答案 0 :(得分:0)

结果在数组中,您应该使用索引

来访问它
$json->results[0]->geometry->location->lat;

检查文档以查看results数组的长度是否与1不同。如果是,则需要迭代它。