【问题标题】:Extract json array data using javascript使用javascript提取json数组数据
【发布时间】:2016-01-20 03:44:03
【问题描述】:

我想使用javascript从以下查询字符串返回的json数据中获取formatted_address。

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true

function getReverseGeocodingData(lat, lng) {
    //use ajax and json
    $.ajax({
        type: "POST",
        url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+sessionStorage.latitude+","+sessionStorage.longitude+"&sensor=true",
        data: jsondata,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var address = response.Result;
            var error = response.Error;

            if (address != null) {
                alert("Found current location: " + address.formatted_address);
                sessionStorage.currentAddress = address.formatted_address;
                return;
            }
        },
        error: function (msg) {
            errorConnection();
        }
    });
}

我尝试获取 formatted_address 但它返回未定义。

【问题讨论】:

  • var address = response.results[0]; 查看我的回答了解更多详情
  • 感谢您的帮助。

标签: javascript arrays json geocode reversegeocodelocation


【解决方案1】:

你可以得到这样的格式化地址(Ajax Success)

   success: function (results) {
        var address = (results[0].formatted_address);
              alert(JSON.stringify(address));
    },

【讨论】:

  • 感谢您的帮助。
【解决方案2】:

只是用这个改了成功函数,你会得到所有格式化的地址

success: function (response) {
    var address = response.results;
    var error = response.error; //write exact word of the "error" which you get in response

    if(address.length > 0) {
        for(i=0; i<address.length; i++) {
            console.log("Found current location: " + address[i].formatted_address);
        }
    }
},

【讨论】:

  • 感谢您的帮助。
【解决方案3】:

抱歉,我之前的回复仅修复了 AJAX 请求- 在请求中指定 contentType 不是必需的,也没有影响 - 这是由服务器控制的。

jQuery 已经将请求解析为对象,在响应上调用 JSON.stringify 导致对象被解析为 JSON 格式的字符串,删除该行解决了问题。似乎也没有必要指定 dataType 。

值得注意的是,您将 lat 和 lng 参数传递给函数但不使用它们 - 我建议添加如下行:

lat = lat || sessionStorage.latitude; 
lng = lng || sessionStorage.longitude; 

下面应该是我的建议的完整解决方案:

function getReverseGeocodingData(lat, lng) {
    lat = lat || sessionStorage.latitude;
    lng = lng || sessionStorage.longitude;

    jsondata = {};
    //use ajax and json
    $.ajax({
        type: "POST",
        url: "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + sessionStorage.latitude + "," + sessionStorage.longitude + "&sensor=true",
        data: jsondata,

        success: function (response) {

            var address = response.results[0].formatted_address;
            var error = response.Error;
            if (address != null) {
                alert("Found current location: " + address);
                sessionStorage.currentAddress = address;
            }
        },
        error: function (msg) {
            errorConnection();
        }
    });
}

【讨论】:

  • 对不起,我之前只用我的答案解决了 ajax 问题 - 我已经更新了我的答案以解决剩余的问题
  • 感谢布赖恩的帮助。
【解决方案4】:

有几个问题需要解决:

显然您在发出GET 请求时不必发布数据。

我重写了您的代码以从latlng 获取参数以便轻松测试。

response 是一个包含results 对象数组的对象(小写r 复数)。每个对象都包含一个formatted_address 属性。

您很可能想要获取第一个:

response.results[0].formatted_address

function getReverseGeocodingData(lat, lng) {
    //use ajax and json
    $.ajax(
    {
        type: "GET",
        url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true",
        dataType: "json",
        success: function (response) {
            alert("Found current location: " + response.results[0].formatted_address);
        },
        error: function (msg) {
            alert('error');
        }
    });
}

尝试一下:

getReverseGeocodingData(44.4647452,7.3553838);

输出:

Found current location: Via Pasubio, 34, 12025 Dronero CN, Italia

【讨论】:

  • 感谢 Paolo 的帮助。
猜你喜欢
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 2021-06-13
  • 2018-06-29
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多