JQuery映射和JSON-获取子数组元素
我正在使用JQuery从geonames获取城市数据。我的问题是,在使用JQuery映射函数时,我想提取子数组项的值 在本例中,如果我想获得Wikipedia链接,我该怎么做 我试过: 维基百科:item.alternateNames.lang['link'] 但没想到它真的会起作用。有人知道谁可以提取子数组项吗 以下是代码位: JSON结果JQuery映射和JSON-获取子数组元素,jquery,json,geonames,Jquery,Json,Geonames,我正在使用JQuery从geonames获取城市数据。我的问题是,在使用JQuery映射函数时,我想提取子数组项的值 在本例中,如果我想获得Wikipedia链接,我该怎么做 我试过: 维基百科:item.alternateNames.lang['link'] 但没想到它真的会起作用。有人知道谁可以提取子数组项吗 以下是代码位: JSON结果 "geonames": [{ "alternateNames": [ { "name": "Rancho Cucamonga", "l
"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']
}
}))
}
})
},
您的
alternateNames
是一个对象数组,因此必须使用索引从数组中检索对象。获得对象后,可以检索“lang”字段:
这应该会返回“链接”。这是假设项
是备选名称
的正确父对象尝试
wikipedia: item.alternateNames[2].name
但是如果你想搜索他们
var name;
$.each( item.alternateNames, function(){
if ( this.lang == 'link' ) {
name = this.name;
return false;
}
});
在同一次事故中,我发现下一个解决方案: 您可以只分配父元素
wikipedia:item.alternateNames
,而不是调用子元素lang
undomap
。之后,您可以在自动完成的select事件中访问它们的嵌套元素
这是我的例子:
$("#Customer_Name ").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("LookupCustomers", "Customers")', type: "POST", dataType: "json",
data: { searchText: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
//respose($.each(data, function (item
return {
label: item.Name,
value: item.Name,
id: item.Id,
phone: item.Phone,
addressStreet: item.AddressStreet,
addressBuilding: item.AddressBuilding,
addressHousing: item.AddressHousing,
addressFlat: item.AddressFlat,
metroStationId: item.MetroStationId,
metroStation: item.MetroStation //trouble was here
}
}))
}
})
},
select: function (event, ui) {
document.getElementById("Customer_Id").value = ui.item.id;
document.getElementById("Customer_Name").value = ui.item.value;
document.getElementById("Customer_Phone").value = ui.item.phone;
document.getElementById("Customer_AddressStreet").value = ui.item.addressStreet;
document.getElementById("Customer_AddressBuilding").value = ui.item.addressBuilding;
document.getElementById("Customer_AddressHousing").value = ui.item.addressHousing;
document.getElementById("Customer_AddressFlat").value = ui.item.addressFlat;
document.getElementById("Customer_MetroStationId").value = ui.item.metroStationId;
document.getElementById("Customer_MetroStation_Name").value = ui.item.metroStation.Name; //and, trouble was here
}
});
顺便说一句,在JSON中,您在adminCode2之前缺少了一个引用。很抱歉,这是由于剪切和粘贴代码造成的,因为我不想将所有数据都放在文章中。根据实际数据,它就在那里。非常感谢,这非常有效。每个条目可能有几个不同的备选名称,因此我需要找到其中一个具有关键链接。
$("#Customer_Name ").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("LookupCustomers", "Customers")', type: "POST", dataType: "json",
data: { searchText: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
//respose($.each(data, function (item
return {
label: item.Name,
value: item.Name,
id: item.Id,
phone: item.Phone,
addressStreet: item.AddressStreet,
addressBuilding: item.AddressBuilding,
addressHousing: item.AddressHousing,
addressFlat: item.AddressFlat,
metroStationId: item.MetroStationId,
metroStation: item.MetroStation //trouble was here
}
}))
}
})
},
select: function (event, ui) {
document.getElementById("Customer_Id").value = ui.item.id;
document.getElementById("Customer_Name").value = ui.item.value;
document.getElementById("Customer_Phone").value = ui.item.phone;
document.getElementById("Customer_AddressStreet").value = ui.item.addressStreet;
document.getElementById("Customer_AddressBuilding").value = ui.item.addressBuilding;
document.getElementById("Customer_AddressHousing").value = ui.item.addressHousing;
document.getElementById("Customer_AddressFlat").value = ui.item.addressFlat;
document.getElementById("Customer_MetroStationId").value = ui.item.metroStationId;
document.getElementById("Customer_MetroStation_Name").value = ui.item.metroStation.Name; //and, trouble was here
}
});