Jquery getJSON返回未定义的

Jquery getJSON返回未定义的,jquery,json,Jquery,Json,我想从返回的JSON格式中获取纬度和经度,但它返回的是未定义的 这是我到目前为止的代码,json格式请看一下URL到底返回了什么 $.ajax({ type: 'GET', url: "http://maps.googleapis.com/maps/api/geocode/json?address=Garibaldi,Prishtina&sensor=true", dataType: 'json', success: function (data) {

我想从返回的JSON格式中获取纬度和经度,但它返回的是未定义的

这是我到目前为止的代码,json格式请看一下URL到底返回了什么

$.ajax({
    type: 'GET',
    url: "http://maps.googleapis.com/maps/api/geocode/json?address=Garibaldi,Prishtina&sensor=true",
    dataType: 'json',
    success: function (data) {

        $.each(data, function () {
            $.each(this, function (key, value) {
                switch (key) {

                    case "lat":
                        alert(value) // access to this node works fine                      
                        break;

                    default:
                        alert(value[0].geometry.location.lat) // this is undefined
                        break;
                }
            });
        });
    }
});
有人知道如何解决这个问题吗?

为什么不

$.ajax({
  type: 'GET',
  url: "http://maps.googleapis.com/maps/api/geocode/json?address=Garibaldi,Prishtina&sensor=true",
  dataType: 'json',
  success: function (data) {
       var loc = data.results[0].geometry.location;
       alert(loc.lat);
       alert(loc.lng);
        }
    });
为什么不

$.ajax({
  type: 'GET',
  url: "http://maps.googleapis.com/maps/api/geocode/json?address=Garibaldi,Prishtina&sensor=true",
  dataType: 'json',
  success: function (data) {
       var loc = data.results[0].geometry.location;
       alert(loc.lat);
       alert(loc.lng);
        }
    });

您应该使用
value.geometry…
而不是
value[0]

索引不在
$内使用。每个
循环。

您应该使用
value.geometry…
而不是
value[0]

索引不在
$中使用。每个
循环。

您应该添加预期的数据结构,而不是引用URL。当您控制台.log(数据)时,您会得到什么;在成功函数内部?为什么
值[0]
?它不应该是
value.geometry…
?是的,Martin u r right它对我有效,没有[0]警报(value.geometry.location.lat)这有效,请添加您的答案,以便我可以接受为正确答案…@Martin这就是原因;写它作为答案?你应该添加预期的数据结构,而不是引用URL。当你控制台.log(数据)时,你会得到什么;在成功函数内部?为什么
值[0]
?它不应该是
value.geometry…
?是的,Martin u r right它对我有效,没有[0]警报(value.geometry.location.lat)这有效,请添加您的答案,以便我可以接受为正确答案…@Martin这就是原因;写下来作为答案?很抱歉,我不能给你正确的答案,因为马丁先回答了,但我认为你的方式更好。我的理解是,如果你认为新的答案更好,你可以更改被接受的答案……)我不是说你应该……只是说这是可能的……很抱歉,我不能给你正确的答案,因为马丁先回答了,但我认为你的方式更好。我的理解是,如果你认为一个新的答案更好,你可以更改被接受的答案……)我不是说你应该……只是说这是可能的。。。。。