2015-04-28 63 views
0

我正在做一個學校項目,我正在使用Google Geocoding API輸出json如何從Json輸出獲取特定信息

我不知道如何在變量中存儲輸出的「formatted_address」(見下文),以便日後可以使用這些信息。

這是我正在使用的json輸出的一部分。

"results" : [ 
    { 
    "address_components" : [ 
     { 
      "long_name" : "11A", 
      "short_name" : "11A", 
      "types" : [ "street_number" ] 
     }, 
     { 
      "long_name" : "Albyn Place", 
      "short_name" : "Albyn Pl", 
      "types" : [ "route" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "AB10 1YE", 
      "short_name" : "AB10 1YE", 
      "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "11A Albyn Place, Aberdeen, Aberdeen City AB10 1YE, UK", 
    "geometry" : { 
     "location" : { 
      "lat" : 57.1425663, 
      "lng" : -2.1144957 
     }, 
     "location_type" : "ROOFTOP", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.1439152802915, 
       "lng" : -2.113146719708498 
      }, 
      "southwest" : { 
       "lat" : 57.1412173197085, 
       "lng" : -2.115844680291502 
      } 
     } 
    }, 
    "place_id" : "ChIJ89KpnycOhEgR96EdolCIM8U", 
    "types" : [ "street_address" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Union Grove (o/s 82)", 
      "short_name" : "Union Grove (o/s 82)", 
      "types" : [ "bus_station", "transit_station", "establishment" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "AB10", 
      "short_name" : "AB10", 
      "types" : [ "postal_code_prefix", "postal_code" ] 
     } 
    ], 
    "formatted_address" : "Union Grove (o/s 82), Aberdeen, Aberdeen, Aberdeen City AB10, UK", 
    "geometry" : { 
     "location" : { 
      "lat" : 57.141483, 
      "lng" : -2.115739 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.1428319802915, 
       "lng" : -2.114390019708498 
      }, 
      "southwest" : { 
       "lat" : 57.1401340197085, 
       "lng" : -2.117087980291502 
      } 
     } 
    }, 
    "place_id" : "ChIJra8KiycOhEgRSR-z6DmOvO8", 
    "types" : [ "bus_station", "transit_station", "establishment" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "AB10 1YE", 
      "short_name" : "AB10 1YE", 
      "types" : [ "postal_code" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen, Aberdeen, Aberdeen City AB10 1YE, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.1429049, 
       "lng" : -2.1140542 
      }, 
      "southwest" : { 
       "lat" : 57.141979, 
       "lng" : -2.1148766 
      } 
     }, 
     "location" : { 
      "lat" : 57.1425663, 
      "lng" : -2.1144957 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.14379093029149, 
       "lng" : -2.113116419708498 
      }, 
      "southwest" : { 
       "lat" : 57.1410929697085, 
       "lng" : -2.115814380291502 
      } 
     } 
    }, 
    "place_id" : "ChIJWey5nycOhEgRXR9pzwov_pY", 
    "types" : [ "postal_code" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "AB10", 
      "short_name" : "AB10", 
      "types" : [ "postal_code_prefix", "postal_code" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen AB10, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.1511959, 
       "lng" : -2.0903783 
      }, 
      "southwest" : { 
       "lat" : 57.1094073, 
       "lng" : -2.1530504 
      } 
     }, 
     "location" : { 
      "lat" : 57.131255, 
      "lng" : -2.1264712 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.1511959, 
       "lng" : -2.0903783 
      }, 
      "southwest" : { 
       "lat" : 57.1094073, 
       "lng" : -2.1530504 
      } 
     } 
    }, 
    "place_id" : "ChIJOT6RmCAOhEgRRCBB7_j2M30", 
    "types" : [ "postal_code_prefix", "postal_code" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "Scotland", 
      "short_name" : "Scotland", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen, Aberdeen City, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.19565069999999, 
       "lng" : -2.0461811 
      }, 
      "southwest" : { 
       "lat" : 57.1041518, 
       "lng" : -2.2058926 
      } 
     }, 
     "location" : { 
      "lat" : 57.149717, 
      "lng" : -2.094278 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.19565069999999, 
       "lng" : -2.0461811 
      }, 
      "southwest" : { 
       "lat" : 57.1041518, 
       "lng" : -2.2058926 
      } 
     } 
    }, 
    "place_id" : "ChIJSXXXH0wFhEgRcsT0XNoFu-g", 
    "types" : [ "locality", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "Scotland", 
      "short_name" : "Scotland", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen City, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.2353521, 
       "lng" : -2.0461828 
      }, 
      "southwest" : { 
       "lat" : 57.07618890000001, 
       "lng" : -2.3609398 
      } 
     }, 
     "location" : { 
      "lat" : 57.1506564, 
      "lng" : -2.1467599 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.2353521, 
       "lng" : -2.0461828 
      }, 
      "southwest" : { 
       "lat" : 57.07618890000001, 
       "lng" : -2.3609398 
      } 
     } 
    }, 
    "place_id" : "ChIJs6qVe2kRhEgRkLBCBhpoDAM", 
    "types" : [ "administrative_area_level_2", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.3274181, 
       "lng" : -2.0076331 
      }, 
      "southwest" : { 
       "lat" : 57.0411197, 
       "lng" : -2.3365801 
      } 
     }, 
     "location" : { 
      "lat" : 57.2177758, 
      "lng" : -2.1823124 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.3274181, 
       "lng" : -2.0076331 
      }, 
      "southwest" : { 
       "lat" : 57.0411197, 
       "lng" : -2.3365801 
      } 
     } 
    }, 
    "place_id" : "ChIJe57LATIShEgRdUZWMa_i0ag", 
    "types" : [ "postal_town" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Scotland", 
      "short_name" : "Scotland", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Scotland, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 60.8607515, 
       "lng" : -0.7246751 
      }, 
      "southwest" : { 
       "lat" : 54.6332381, 
       "lng" : -8.6498565 
      } 
     }, 
     "location" : { 
      "lat" : 56.49067119999999, 
      "lng" : -4.2026458 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 60.84567610000001, 
       "lng" : -0.7247473999999999 
      }, 
      "southwest" : { 
       "lat" : 54.6332381, 
       "lng" : -8.6492466 
      } 
     } 
    }, 
    "place_id" : "ChIJn6HyA8TiYUgRFAfDCdj6wec", 
    "types" : [ "administrative_area_level_1", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "United Kingdom", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 60.8606697, 
       "lng" : 33.916555 
      }, 
      "southwest" : { 
       "lat" : 34.5626034, 
       "lng" : -8.649357199999999 
      } 
     }, 
     "location" : { 
      "lat" : 55.378051, 
      "lng" : -3.435973 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 60.856553, 
       "lng" : 1.7627096 
      }, 
      "southwest" : { 
       "lat" : 49.8669688, 
       "lng" : -8.649357199999999 
      } 
     } 
    }, 
    "place_id" : "ChIJqZHHQhE7WgIReiWIMkOg-MQ", 
    "types" : [ "country", "political" ] 
    } 
], 
"status" : "OK" 

到目前爲止,我在我的js文件嘗試這樣的:

var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY; 
//alert(restourantAddressRequest.results.formatted_address); 
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address); 
+1

如果添加了更多的細節你的問題,我我相信很多人都能幫上忙。你如何存儲數據? MySql,文本,瀏覽器存儲,會話... brail? – HappyCoder

+0

感謝您的回覆。我會在這個問題上添加更多細節。 –

+2

請添加實際的JSON輸出,而不是screenShot。 –

回答

1

你需要訪問它之前,你解析JSON響應:

var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY"; 
var json= JSON.parse(restourantAddressRequest); 
//alert(restourantAddressRequest.results.formatted_address); 
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address); 

希望這會工作。

+0

它似乎是這樣做的。謝謝! :) –

2

你想用點符號來達到你想要的值。谷歌的地理編碼V3返回數組,所以你要查找的內容如下:

results[0].formatted_address 

這裏是一個提醒格式的地址給你一個小提琴:

http://jsfiddle.net/iamjpg/h39041w2/

我希望這是很有幫助。

+0

哇。感謝那!這是比我想象的方式更簡單:) –

+0

當我嘗試這個restaurantAddressRequest.results [0] .formatted_address它沒有工作:/ –

0

我用getJSON方法的jQuery這樣解決問題:

var restaurantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY"; 
//alert(restourantAddressRequest.results.formatted_address); 

$.getJSON(restaurantAddressRequest,function(jsondata){ 
    $("#restaurant_address").html("Address: " + jsondata.results[0].jsondata.results[0].formatted_address); 
}); 
1

如果你想以異步方式做到這一點:

//Show that you are loading the data 

$("#restaurant_address").html("Address: Loading... ");  

//Set the url: 

var url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY"'; 

//Make the Ajax call: 


     $.ajax({ 
      type: 'GET', 
      async: true, 
      url: url, 
      dataType: "json", 

      success: function(responseObject){ 


       if (responseObject.success) 
       { 
        $("#restaurant_address").html("Address: " + ...); 

       } else { 
        $("#restaurant_address").html("Address: n/a"); 
       } 

      } 
     });