gpt4 book ai didi

javascript - 如何从Json输出中获取特定信息

转载 作者:行者123 更新时间:2023-11-28 15:24:06 25 4
gpt4 key购买 nike

我正在做一个学校项目,并且正在使用 Google Geocoding API 的 json 输出。

我不知道如何将输出的“formatted_address”(见下文)存储在变量中,以便稍后使用此信息。

这是我正在使用的 json 输出的一部分。

"results" : [
{
"address_components" : [
{
"long_name" : "11A",
"short_name" : "11A",
"types" : [ "street_number" ]
},
{
"long_name" : "Albyn Place",
"short_name" : "Albyn Pl",
"types" : [ "route" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "AB10 1YE",
"short_name" : "AB10 1YE",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "11A Albyn Place, Aberdeen, Aberdeen City AB10 1YE, UK",
"geometry" : {
"location" : {
"lat" : 57.1425663,
"lng" : -2.1144957
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 57.1439152802915,
"lng" : -2.113146719708498
},
"southwest" : {
"lat" : 57.1412173197085,
"lng" : -2.115844680291502
}
}
},
"place_id" : "ChIJ89KpnycOhEgR96EdolCIM8U",
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Union Grove (o/s 82)",
"short_name" : "Union Grove (o/s 82)",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "AB10",
"short_name" : "AB10",
"types" : [ "postal_code_prefix", "postal_code" ]
}
],
"formatted_address" : "Union Grove (o/s 82), Aberdeen, Aberdeen, Aberdeen City AB10, UK",
"geometry" : {
"location" : {
"lat" : 57.141483,
"lng" : -2.115739
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.1428319802915,
"lng" : -2.114390019708498
},
"southwest" : {
"lat" : 57.1401340197085,
"lng" : -2.117087980291502
}
}
},
"place_id" : "ChIJra8KiycOhEgRSR-z6DmOvO8",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "AB10 1YE",
"short_name" : "AB10 1YE",
"types" : [ "postal_code" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen, Aberdeen, Aberdeen City AB10 1YE, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.1429049,
"lng" : -2.1140542
},
"southwest" : {
"lat" : 57.141979,
"lng" : -2.1148766
}
},
"location" : {
"lat" : 57.1425663,
"lng" : -2.1144957
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.14379093029149,
"lng" : -2.113116419708498
},
"southwest" : {
"lat" : 57.1410929697085,
"lng" : -2.115814380291502
}
}
},
"place_id" : "ChIJWey5nycOhEgRXR9pzwov_pY",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "AB10",
"short_name" : "AB10",
"types" : [ "postal_code_prefix", "postal_code" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen AB10, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.1511959,
"lng" : -2.0903783
},
"southwest" : {
"lat" : 57.1094073,
"lng" : -2.1530504
}
},
"location" : {
"lat" : 57.131255,
"lng" : -2.1264712
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.1511959,
"lng" : -2.0903783
},
"southwest" : {
"lat" : 57.1094073,
"lng" : -2.1530504
}
}
},
"place_id" : "ChIJOT6RmCAOhEgRRCBB7_j2M30",
"types" : [ "postal_code_prefix", "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Scotland",
"short_name" : "Scotland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen, Aberdeen City, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.19565069999999,
"lng" : -2.0461811
},
"southwest" : {
"lat" : 57.1041518,
"lng" : -2.2058926
}
},
"location" : {
"lat" : 57.149717,
"lng" : -2.094278
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.19565069999999,
"lng" : -2.0461811
},
"southwest" : {
"lat" : 57.1041518,
"lng" : -2.2058926
}
}
},
"place_id" : "ChIJSXXXH0wFhEgRcsT0XNoFu-g",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Scotland",
"short_name" : "Scotland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen City, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.2353521,
"lng" : -2.0461828
},
"southwest" : {
"lat" : 57.07618890000001,
"lng" : -2.3609398
}
},
"location" : {
"lat" : 57.1506564,
"lng" : -2.1467599
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.2353521,
"lng" : -2.0461828
},
"southwest" : {
"lat" : 57.07618890000001,
"lng" : -2.3609398
}
}
},
"place_id" : "ChIJs6qVe2kRhEgRkLBCBhpoDAM",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.3274181,
"lng" : -2.0076331
},
"southwest" : {
"lat" : 57.0411197,
"lng" : -2.3365801
}
},
"location" : {
"lat" : 57.2177758,
"lng" : -2.1823124
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.3274181,
"lng" : -2.0076331
},
"southwest" : {
"lat" : 57.0411197,
"lng" : -2.3365801
}
}
},
"place_id" : "ChIJe57LATIShEgRdUZWMa_i0ag",
"types" : [ "postal_town" ]
},
{
"address_components" : [
{
"long_name" : "Scotland",
"short_name" : "Scotland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Scotland, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 60.8607515,
"lng" : -0.7246751
},
"southwest" : {
"lat" : 54.6332381,
"lng" : -8.6498565
}
},
"location" : {
"lat" : 56.49067119999999,
"lng" : -4.2026458
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 60.84567610000001,
"lng" : -0.7247473999999999
},
"southwest" : {
"lat" : 54.6332381,
"lng" : -8.6492466
}
}
},
"place_id" : "ChIJn6HyA8TiYUgRFAfDCdj6wec",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "United Kingdom",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 60.8606697,
"lng" : 33.916555
},
"southwest" : {
"lat" : 34.5626034,
"lng" : -8.649357199999999
}
},
"location" : {
"lat" : 55.378051,
"lng" : -3.435973
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 60.856553,
"lng" : 1.7627096
},
"southwest" : {
"lat" : 49.8669688,
"lng" : -8.649357199999999
}
}
},
"place_id" : "ChIJqZHHQhE7WgIReiWIMkOg-MQ",
"types" : [ "country", "political" ]
}
],
"status" : "OK"

到目前为止,我正在我的 js 文件中尝试这个:

var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY;
//alert(restourantAddressRequest.results.formatted_address);
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address);

最佳答案

您想使用点表示法来获得您想要的值。 Google V3 地理编码器返回一个数组,因此您要查找的内容如下:

results[0].formatted_address

这里有一个 fiddle ,可以提醒您格式化地址:

http://jsfiddle.net/iamjpg/h39041w2/

希望这对您有所帮助。

关于javascript - 如何从Json输出中获取特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29926720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com