简体   繁体   English

如何从json检索这些值?

[英]How do I retrieve these values from json?

I am trying to retrieve the latitude and longitude of a location from google maps via jquery ajax. 我正在尝试通过jquery ajax从Google地图检索位置的纬度和经度。

Here's my JavaScript code: 这是我的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;
    }

When I previewed the response body from my console I got the json data returned as : 当我从控制台预览响应正文时,我得到的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" 
} 

I am trying to get the values of lat and lng from location but I get an error: 我正在尝试从位置获取lat和lng的值,但出现错误:

TypeError: json_data.results.geometry is undefined

I have looked at this post , this post , this post , this post and this post but I don't really know what to do. 我看过这篇文章这篇文章这篇文章这篇文章这篇文章,但是我真的不知道该怎么做。 They all seem unrelated. 它们似乎无关。 Please help I am still new to json. 请帮助我对JSON还是陌生的。 Thanks. 谢谢。

what about this: 那这个呢:

lat = json_data.results[0].geometry.location.lat;
lng = json_data.results[0].geometry.location.lng;
success: function(json_data)
            {
                lat = json_data.results[0].geometry.location.lat;
                lng = json_data.results[0].geometry.location.lng;
                ret = true;
            }

Results is an array, you also might want to include a some logic to handle the case in which multiple results are returned. 结果是一个数组,您可能还想包括一些逻辑以处理返回多个结果的情况。 /edited removed snippet /已删除的摘要

结果是JSON响应中的数组,因此您必须首先访问该数组的元素:

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

Your result is array. 您的结果是数组。

So you have to get values of lat/lng by the following way: 因此,您必须通过以下方式获取lat / lng的值:

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

and

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

Using json.parse will turn it into an object that you can use. 使用json.parse会将其变成您可以使用的对象。

Try: 尝试:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM