【问题标题】:Nativescript/Google Geocoding JSON ParseNativescript/Google 地理编码 JSON 解析
【发布时间】:2017-12-14 21:40:50
【问题描述】:

我正在从我的 Nativescript 代码中读取来自 Google API 的地理编码。结果是这样来的:

{"_bodyInit":"{\n   \"results\" : [\n      {\n         \"address_components\" : [\n            {\n               \"long_name\" : \"Cooper City\",\n               \"short_name\" : \"Cooper City\",\n               \"types\" : [ \"locality\", \"political\" ]\n            },\n            {\n               \"long_name\" : \"Broward County\",\n               \"short_name\" : \"Broward County\",\n               \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n            },\n            {\n               \"long_name\" : \"Florida\",\n               \"short_name\" : \"FL\",\n               \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n            },\n            {\n               \"long_name\" : \"United States\",\n               \"short_name\" : \"US\",\n               \"types\" : [ \"country\", \"political\" ]\n            }\n         ],\n         \"formatted_address\" : \"Cooper City, FL, USA\",\n         \"geometry\" : {\n

读取这段代码时:

fetchModule.fetch(geoPlace, {
    method: "GET"
})
.then(function(response) {
    alert({title: "GET Response", message: JSON.stringify(response), okButtonText: "Close"});
    console.log(JSON.stringify(response))
}, function(error) {
    console.log(JSON.stringify(error));
})

我的问题是,例如,如何访问“库珀城”?

我试过了(没有成功):

console.log(response.value["results"])
console.log(response[0])
console.log(response.results[0])

【问题讨论】:

    标签: json api nativescript reverse-geocoding


    【解决方案1】:

    修改响应我发现它不是一个成功的 JSON 格式,您应该寻找将其转换为 JSON 格式的方法,以便能够使用其代码并获取您需要的标签。

    https://jsonformatter.curiousconcept.com/

    【讨论】:

    • 这可能是因为他已经 JSON.stringifying 一个 JSON。
    【解决方案2】:

    这是我找到解决方案的方法:

    var m = JSON.parse(response._bodyInit)
    console.log(m.results[0].address_components[1].short_name)
    

    由于某种原因,我不知道原始结果没有作为有效的 JSON 返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-06
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      相关资源
      最近更新 更多