我正在使用 Javascript 通过 GET 获取 JSON 对象。这是 Google 地球 ( https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests ) 中反向地理编码的结果。
{
"results" : [
{
"address_components" : [
{
"long_name" : "1",
"short_name" : "1",
"types" : [ "street_number" ]
},
{
"long_name" : "Church Street",
"short_name" : "Church St",
"types" : [ "route" ]
},
{
"long_name" : "Mid-Cambridge",
"short_name" : "Mid-Cambridge",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Cambridge",
"short_name" : "Cambridge",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Cambridge",
"short_name" : "Cambridge",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Middlesex",
"short_name" : "Middlesex",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Massachusetts",
"short_name" : "MA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "02138",
"short_name" : "02138",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1 Church Street, Cambridge, MA 02138, USA",
"geometry" : {
"location" : {
"lat" : 42.37440120,
"lng" : -71.11850609999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 42.37575018029150,
"lng" : -71.11715711970849
},
"southwest" : {
"lat" : 42.37305221970851,
"lng" : -71.11985508029150
}
}
},
"postcode_localities" : [],
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
在说明中,他们告诉我们像这样解析 JSON(使用 JS):
for (i = 0; i < myJSONResult.results.length; i++) {
myAddress[i] = myJSONResult.results[i].formatted_address;
}
但是,它不起作用。我假设 myAddress 是一个之前已经定义过的数组,其中没有任何内容,并且 myJSONResult 是您用于存储来自 HTTP 请求的响应的变量。如何获取 formatted_address 字符串?