如何对GeoCode get()请求的JSON进行排序

时间:2018-04-13 14:46:37

标签: javascript json parsing google-geocoder

我当前有以下代码来处理get请求并返回一个地址。

address = "https://maps.googleapis.com/maps/api/geocode/json?latlng=38.8976763,-77.0387238&key=[APIKEY]"

results = httpGet(address)
console.log(results)

function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

代码执行时,以JSON格式返回以下内容。我怎么能抓住" formatted_address"字段来自以下文字?

{
"results" : [
  {
     "address_components" : [
        {
           "long_name" : "1650",
           "short_name" : "1650",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Pennsylvania Avenue Northwest",
           "short_name" : "Pennsylvania Ave NW",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Northwest Washington",
           "short_name" : "Northwest Washington",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Washington",
           "short_name" : "Washington",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "District of Columbia",
           "short_name" : "DC",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "20504",
           "short_name" : "20504",
           "types" : [ "postal_code" ]
        }
     ]
         "formatted_address" : "1650 Pennsylvania Ave NW, Washington, DC 20504, USA",
     "geometry" : {
        "location" : {
           "lat" : 38.8980085,
           "lng" : -77.0389457
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 38.8993574802915,
              "lng" : -77.0375967197085
           },
           "southwest" : {
              "lat" : 38.8966595197085,
              "lng" : -77.04029468029151
           }
        }
     },
     "place_id" : "ChIJY7LRgby3t4kRz3_VtbtJpfE",
     "types" : [ "street_address" ]
  },
}

我尝试以通常处理JSON文件的方式处理文件,但我仍然不熟悉正确的过程。会使用类似

的东西
JSON.parse(result) 

在我尝试对输出进行排序时提供帮助?任何帮助,将不胜感激。

同样,如何修改我的代码以使用以下代码段?被告知不鼓励同步请求,我应该努力创建一个异步请求,但我不知道如何集成它。

 function httpGetAsync(theUrl, callback)
 {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}

2 个答案:

答案 0 :(得分:0)

您的数据已被破解。但这很有效。添加了之前的formatted_address和底部的括号

const data = { "results" : [
  {
     "address_components" : [
        {
           "long_name" : "1650",
           "short_name" : "1650",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Pennsylvania Avenue Northwest",
           "short_name" : "Pennsylvania Ave NW",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Northwest Washington",
           "short_name" : "Northwest Washington",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Washington",
           "short_name" : "Washington",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "District of Columbia",
           "short_name" : "DC",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "20504",
           "short_name" : "20504",
           "types" : [ "postal_code" ]
        }
     ],
    "formatted_address" : "1650 Pennsylvania Ave NW, Washington, DC 20504, USA",
     "geometry" : {
        "location" : {
           "lat" : 38.8980085,
           "lng" : -77.0389457
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 38.8993574802915,
              "lng" : -77.0375967197085
           },
           "southwest" : {
              "lat" : 38.8966595197085,
              "lng" : -77.04029468029151
           }
        }
     },
     "place_id" : "ChIJY7LRgby3t4kRz3_VtbtJpfE",
     "types" : [ "street_address" ]
  },
]
}

获取formatted_address

console.log(data.results[0].formatted_address)

另外,你可以试试请求& request-promise(同时安装)

const request = require('request-promise')

request('https://maps.googleapis.com/maps/api/geocode/json?latlng=38.8976763,-77.0387238&key=[APIKEY]').then(res => {
  console.log(res.results[0].formatted_address)
})

答案 1 :(得分:0)

在我正确解析之前,必须先将JSON.parse输出。结尾使用以下代码:

function getAddress(){
   apiKey = "Key";
   lat = 38.8976763;
   lon = -77.0387238;
   address = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lon + "&key=" + apiKey;

   const request = require('request-promise')
   request(address).then(res => {
   res = JSON.parse(res)
   console.log(res.results[0].formatted_address)
   })
}

getAddress()