2017-01-12 81 views
0

我想解析來自Google地圖的響應,這是JSON響應。解析Json Google GeoMaps

{ "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" } 

如果我想例如回顯幾何(拉特和lng),我該怎麼做。

我曾嘗試:($內容包含上述響應)

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

'results'是一個數組 – AxelH

+0

你應該迭代結果,因爲它是一個數組。之後進入geometry-> location-> lat; – Milaci

回答

0

結果是一個數組,你應該使用索引

$json->results[0]->geometry->location->lat; 

訪問檢查文件,看是否results數組的長度可能不等於1.如果是,則需要遍歷它。