使用JS

时间:2016-04-02 21:39:06

标签: javascript jquery json

因为我现在尝试了大约10个小时,所以我会感激任何解决方案。 我需要一个JSON的特定值,但不知道如何选择它。

所以这是我的JSON

    {
    "results" : [
        {
     "address_components" : [
        {
           "long_name" : "Buchenberg",
           "short_name" : "Buchenberg",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Oberallgäu",
           "short_name" : "Oberallgäu",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Swabia",
           "short_name" : "Swabia",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Bavaria",
           "short_name" : "BY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Germany",
           "short_name" : "DE",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Buchenberg, Germany",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 47.7525249,
              "lng" : 10.286058
           },
           "southwest" : {
              "lat" : 47.6694625,
              "lng" : 10.1128175
           }
        },
        "location" : {
           "lat" : 47.6960163,
           "lng" : 10.239696
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 47.7525249,
              "lng" : 10.286058
           },
           "southwest" : {
              "lat" : 47.6694625,
              "lng" : 10.1128175
           }
        }
     },
     "place_id" : "ChIJA6IwOqmAm0cRxVEUeHkZnrg",
     "types" : [ "locality", "political" ]
    }
  ],
  "status" : "OK"
}

我需要从格式化的地址 - > geometry-> bounds-> northeast获取lat / lng的数据。

我试过这个JS代码

     $.getJSON("https://maps.googleapis.com/maps/api/geocode/json?address=buchenberg", function(result){
         var geoArray = result;
         alert(geoArray['status']);     
     });

它会返回" OK",就像它应该的那样。 但是我如何选择lat / lng,因为它涉及到这个数组的所有深度和所有这些括号:D请帮助我

3 个答案:

答案 0 :(得分:0)

试试这个会起作用:

 // Latitude.
 var lat = Obj.results[0].geometry.bounds.northeast.lat;
 // Longitude.
 var lng = Obj.results[0].geometry.bounds.northeast.lng;

在控制台中打印:

console.log(lat);
console.log(lng);

JsFiddle: https://jsfiddle.net/n7voyo6t/

答案 1 :(得分:0)

你的url在索引0的数组中返回结果,所以你需要先访问它,才能访问几何对象的属性

    var lat = geoArray.results[0].geometry.bounds.northeast.lat;
    var long = geoArray.results[0].geometry.bounds.northeast.lng;



var result =   {
    "results" : [
        {
     "address_components" : [
        {
           "long_name" : "Buchenberg",
           "short_name" : "Buchenberg",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Oberallgäu",
           "short_name" : "Oberallgäu",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Swabia",
           "short_name" : "Swabia",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Bavaria",
           "short_name" : "BY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Germany",
           "short_name" : "DE",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Buchenberg, Germany",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 47.7525249,
              "lng" : 10.286058
           },
           "southwest" : {
              "lat" : 47.6694625,
              "lng" : 10.1128175
           }
        },
        "location" : {
           "lat" : 47.6960163,
           "lng" : 10.239696
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 47.7525249,
              "lng" : 10.286058
           },
           "southwest" : {
              "lat" : 47.6694625,
              "lng" : 10.1128175
           }
        }
     },
     "place_id" : "ChIJA6IwOqmAm0cRxVEUeHkZnrg",
     "types" : [ "locality", "political" ]
    }
  ],
  "status" : "OK"
};
var geoArray = result;
var lat = geoArray.results[0].geometry.bounds.northeast.lat;
var long = geoArray.results[0].geometry.bounds.northeast.lng;

console.log(lat);
console.log(long);




输出:

  lat  47.7525249
  long  10.286058

答案 2 :(得分:-1)

下面的代码将从bounds对象中的northeast键中提取lat / long。

 alert(geoArray[0].geometry.bounds.northeast);

另外,你可以这样写:

 alert(geoArray[0]['geometry']['bounds']['northeast']);