2010-09-16 54 views
1

我使用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'] 

         } 
        })) 
       } 
      }) 
     }, 
+0

作爲一個方面說明,在JSON丟失的adminCode2之前的報價。 – 2010-09-16 19:23:36

+0

不好意思,那是因爲我不想把所有的數據都放在帖子裏,所以來自剪切和粘貼代碼。關於它的實際數據。 – Frank 2010-09-16 19:26:31

回答

3

嘗試...

wikipedia: item.alternateNames[2].name 

但是,如果你想查詢他們...

var name; 
$.each(item.alternateNames, function(){ 
    if (this.lang == 'link') { 
    name = this.name; 
    return false; 
    } 
}); 
+0

非常感謝這非常完美。每個條目可能有幾個不同的備用名稱,所以我需要找到與他們的關鍵鏈接。 – Frank 2010-09-16 19:39:19

+0

是的,我想象的一樣多。很高興它的工作。 – BBonifield 2010-09-16 19:40:44

0

alternateNames是對象的數組,所以你就必須使用索引來從數組中檢索一個對象。一旦你的對象,你可以檢索「郎」字段:

item.alternateNames[2].lang 

應該回報你「鏈接」。這是假設itemalternateNames的正確父對象。

0

在同一事故,我覺得未來解決:

lang撤銷而不是調用子元素map,你只能分配父母元素wikipedia: item.alternateNames。之後,您可以在選擇自動完成事件時訪問它們的嵌套元素。

這是我的例子:

$("#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 
      } 
     });