使用JS解析JSON文件(URL)

parsing JSON file (URL) with JS

本文关键字:URL 文件 JSON JS 解析 使用      更新时间:2023-11-12

由于我已经尝试了大约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"
}

我需要从格式化地址->几何体->边界->东北获得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请帮助我

您的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

试试这个就行了:

 // 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/

下面的代码将从bound对象内的东北键提取lat/long。

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

另外,你可以这样写:

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