【问题标题】:How do I retrieve these values from json?如何从 json 中检索这些值?
【发布时间】:2014-10-26 06:19:32
【问题描述】:

我正在尝试通过 jquery ajax 从谷歌地图中检索某个位置的纬度和经度。

这是我的 JavaScript 代码:

    var lat;
    var lng;

    function setCordinates(address)
    {
        var ret = false;
        data = "address="+address;
        $.ajax({
            type: "GET",
            url: "https://maps.googleapis.com/maps/api/geocode/json",
            data: data,
            cache: false,
            dataType: 'json',
            success: function(json_data)
            {
                lat = json_data.results.geometry.location.lat;
                lng = json_data.results.geometry.location.lng;
                ret = true;
            },
            error: function()
            { 
                alert("There was an error!");
            }
        });
        return ret;
    }

当我从控制台预览响应正文时,我得到的 json 数据返回为:

{
    "results" : [ 
        { 
            "address_components" : [ 
                { 
                    "long_name" : "Uyo", 
                    "short_name" : "Uyo", 
                    "types" : [ "locality", "political" ] 
                }, 
                { 
                    "long_name" : "Akwa Ibom", 
                    "short_name" : "Akwa Ibom", 
                    "types" : [ "administrative_area_level_1", "political" ] 
                }, 
                { 
                    "long_name" : "Nigeria", 
                    "short_name" : "NG", 
                    "types" : [ "country", "political" ] 
                } 
            ], 
            "formatted_address" : "Uyo, Nigeria", 
            "geometry" : 
            { 
                "bounds" : 
                { 
                    "northeast" : 
                    { 
                        "lat" : 5.0906023, 
                        "lng" : 8.0030251 
                    }, 
                    "southwest" : 
                    { 
                        "lat" : 4.9681658, 
                        "lng" : 7.8638077 
                    } 
                }, 
                "location" : 
                { 
                    "lat" : 5.0377396, 
                    "lng" : 7.9127945 
                }, 
                "location_type" : "APPROXIMATE", 
                "viewport" : 
                { 
                    "northeast" : 
                    { 
                        "lat" : 5.0906023, 
                        "lng" : 8.0030251 
                    }, 
                    "southwest" : 
                    { 
                        "lat" : 4.9681658, 
                        "lng" : 7.8638077 
                    } 
                } 
            }, 
            "partial_match" : true, 
            "types" : [ "locality", "political" ] 
        } 
    ], 
    "status" : "OK" 
} 

我正在尝试从位置获取 lat 和 lng 的值,但出现错误:

TypeError: json_data.results.geometry is undefined

我看过this postthis postthis postthis postthis post,但我真的不知道该怎么做。它们似乎都无关紧要。请帮助我对json还是陌生的。谢谢。

【问题讨论】:

  • json_data.results 是一个数组,所以需要包含元素的索引。它只有一个元素,所以json_data.results[0].geometry

标签: javascript jquery ajax json google-maps


【解决方案1】:

这个呢:

lat = json_data.results[0].geometry.location.lat;
lng = json_data.results[0].geometry.location.lng;

【讨论】:

    【解决方案2】:

    results 是 JSON 响应中的一个数组,因此您必须先访问该数组的一个元素:

    lat = json_data.results[0].geometry.location.lat;
    

    【讨论】:

      【解决方案3】:
      success: function(json_data)
                  {
                      lat = json_data.results[0].geometry.location.lat;
                      lng = json_data.results[0].geometry.location.lng;
                      ret = true;
                  }
      

      Results 是一个数组,您可能还希望包含一些逻辑来处理返回多个结果的情况。 /edited 删除了 sn-p

      【讨论】:

        【解决方案4】:

        你的结果是数组。

        所以你必须通过以下方式获取 lat/lng 的值:

        json_data.results[0].geometry.location.lat 
        

        json_data.results[0].geometry.location.lng
        

        【讨论】:

          【解决方案5】:

          使用 json.parse 会把它变成你可以使用的对象。

          试试:

          var json_dataObject = JSON.Parse(json_data);
          var lat = json_dataObject.results[0].geometry.location.lat;
          var lat = json_dataObject.results[0].geometry.location.lng;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-19
            • 1970-01-01
            相关资源
            最近更新 更多