JQuery を使用して、ジオネームから都市データを取得しています。私の問題は、JQuery マップ関数を使用するときにサブ配列項目の値を抽出したい場合です。
この例で、ウィキペディアのリンクを取得したい場合、どうすればよいでしょうか?
私は試しました: ウィキペディア: item.alternateNames.lang['link']
しかし、それが本当にうまくいくとは思っていませんでした。サブ配列アイテムを抽出できる人を知っている人はいますか?
コードのビットは次のとおりです。
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']
}
}))
}
})
},