我有这个json。我正在使用Google提供的反向地理编码API。我在给定的网址中点击了获取请求,这是我得到的响应。
{
"results": [
{
"address_components": [
{
"long_name": "Goth Haji Umed Ali Gabole-Konker Road",
"short_name": "Goth Haji Umed Ali Gabole-Konker Rd",
"types": [
"route"
]
},
{
"long_name": "Haji Umaid Ali Goth",
"short_name": "Haji Umaid Ali Goth",
"types": [
"political",
"sublocality",
"sublocality_level_2"
]
},
{
"long_name": "Gadap",
"short_name": "Gadap",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Karachi",
"short_name": "Karachi",
"types": [
"locality",
"political"
]
},
], "status": "OK"}
这是我的代码
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&key=AIzaSyBcV_CkYu5VvJb1ZZF8GWCAmyzhedGpYzk";
jQuery.get(url, function (result)
{
console.log(result);
}
);
}
JSON在我的控制台中 。 我想提醒城市名称“卡拉奇”。我怎么能得到这个?
答案 0 :(得分:0)
试试这个:
var jsonObj = {
"results": [{
"address_components": [{
"long_name": "Goth Haji Umed Ali Gabole-Konker Road",
"short_name": "Goth Haji Umed Ali Gabole-Konker Rd",
"types": [
"route"
]
},
{
"long_name": "Haji Umaid Ali Goth",
"short_name": "Haji Umaid Ali Goth",
"types": [
"political",
"sublocality",
"sublocality_level_2"
]
},
{
"long_name": "Gadap",
"short_name": "Gadap",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "Karachi",
"short_name": "Karachi",
"types": [
"locality",
"political"
]
}
],
"status": "OK"
}]
};
var res = jsonObj.results[0].address_components.filter(obj => obj.long_name == 'Karachi');
console.log(res[0].long_name);