我使用的是位置API,并希望访问adminArea1
值,以便我可以将国家/地区存储在JavaScript变量中。在这种情况下,我如何访问该值?我已经知道如何将这个JSON导入JavaScript(作为对象),所以这没问题。该对象称为locationResults
。
{
"results": [{
"locations": [{
"latLng": {
"lng": -76.329999,
"lat": 40.0755
},
"adminArea4": "Lancaster County",
"adminArea5Type": "City",
"adminArea4Type": "County",
"adminArea5": "Lancaster",
"street": "Granite Run Drive",
"adminArea1": "US", //THE VALUE I WANT
"adminArea3": "PA",
"type": "s",
"displayLatLng": {
"lng": -76.333129,
"lat": 40.07657
},
"linkId": 0,
"postalCode": "17601",
"sideOfStreet": "N",
"dragPoint": false,
"adminArea1Type": "Country",
"geocodeQuality": "ADDRESS",
"geocodeQualityCode": "L1AAA",
"mapUrl": "http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luur2du2n1,bn=o5-9ar2l0&type=map&size=225,160&pois=purple-1,40.0755,-76.329999,0,0|¢er=40.0755,-76.329999&zoom=15&rand=-320722776",
"adminArea3Type": "State"
}],
"providedLocation": {
"latLng": {
"lng": -76.329999,
"lat": 40.0755
}
}
}],
"options": {
"ignoreLatLngInput": false,
"maxResults": -1,
"thumbMaps": true
},
"info": {
"copyright": {
"text": "© 2014 MapQuest, Inc.",
"imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
"imageAltText": "© 2014 MapQuest, Inc."
},
"statuscode": 0,
"messages": []
}
}
使用以下内容:
$.getJSON(reverseGeoRequestURL, function(reverseGeoResult){
window.country = reverseGeoResult.results[0].adminArea1;
console.log(window.country);
});
提供控制台输出" 未定义"。
这个JSON肯定是凌乱的!我想出了如何获得价值:
locationResults.results[0].locations[0].adminArea1;
答案 0 :(得分:0)
locationResults.results[0].adminArea1
假设你想要第一个并且你已经检查了locationResults.length > 0
。
使用点运算符或包含运算符且属性名称为字符串的js访问对象的属性。有财产,它将是:
locationResults.results[0]['adminArea1']