我使用JQuery從geonames獲取城市數據。我的問題是,當我想使用JQuery映射函數提取子數組項的值時。JQuery地圖和JSON - 獲取子數組元素
在這個例子中,如果我想獲得維基百科鏈接,我該怎麼做?
我想: 維基百科:item.alternateNames.lang [ '鏈接']
但沒想到它會真正發揮作用。有誰知道我可以提取子數組項嗎?
以下是代碼位:
JSON結果
"geonames": [{
"alternateNames": [
{
"name": "Rancho Cucamonga",
"lang": "en"
},
{
"name": "91739",
"lang": "post"
},
{
"name": "http://en.wikipedia.org/wiki/Rancho_Cucamonga%2C_California",
"lang": "link"
}
],
adminCode2": "071",
"countryName": "United States",
"adminCode1": "CA",
"fclName": "city, village,...",
"elevation": 368,
"score": 0.9999999403953552,
"countryCode": "US",
"lng": -117.5931084,
"adminName2": "San Bernardino County",
"adminName3": "",
"fcodeName": "populated place",
JQuery的:
$("#city").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function(data) {
response($.map(data.geonames, function(item) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name,
latitude: item.lat,
longitude: item.lng,
// problem here: how to get the value for the alternateNames.lang where lang = link
wikipedia: item.alternateNames.lang['link']
}
}))
}
})
},
作爲一個方面說明,在JSON丟失的adminCode2之前的報價。 – 2010-09-16 19:23:36
不好意思,那是因爲我不想把所有的數據都放在帖子裏,所以來自剪切和粘貼代碼。關於它的實際數據。 – Frank 2010-09-16 19:26:31