2012-05-16 30 views
2

這應該是非常直截了當的,但即使看看其他問題,我也無法得到它的工作。世界銀行JSONP解析

我從以下鏈接獲取一些樣本世界銀行的數據:

World Bank Query

這將返回以下JSONP結構:

getWorldBankData([ 
{ 
    "page": 1, 
    "pages": 1, 
    "per_page": "100", 
    "total": 52 
}, 
[ 
    { 
     "indicator": { 
      "id": "DC.DAC.FINL.CD", 
      "value": "Net bilateral aid flows from DAC donors, Finland (current US$)" 
     }, 
     "country": { 
      "id": "GB", 
      "value": "United Kingdom" 
     }, 
     "value": null, 
     "decimal": "0", 
     "date": "2011" 
    }, 
    { 
     "indicator": { 
      "id": "DC.DAC.FINL.CD", 
      "value": "Net bilateral aid flows from DAC donors, Finland (current US$)" 
     }, 
     "country": { 
      "id": "GB", 
      "value": "United Kingdom" 
     }, 
     "value": null, 
     "decimal": "0", 
     "date": "2010" 
    }, 

我希望得到國家的稱號。我曾試圖用下面的代碼可以這樣做:

function getWorldBankData(json){ 
    $.each(json.country ,function(){ 
     var country = "<option>"+this.value+"</option>" 
     $('#category').append(country) 
    }); 
} 

,但我得到了以下錯誤:

a is undefined 
f()jquery.min.js (line 16) 
a = undefined 
c = function() 
d = undefined 
getWorldBankData()oil.js (line 11) 
json = [Object { page=1, pages=1, per_page="100", more...}, [Object { indicator={...}, country={...}, decimal="0", more...}, Object { indicator={...}, country={...}, decimal="0", more...}, Object { indicator={...}, country={...}, decimal="0", more...}, 49 more...]] 
DC.DAC.FINL.CD?per_page=100&date=1960:2012&format=jsonP&prefix=getWorldBankData()DC.DAC...ankData (line 1) 
[Break On This Error] 

...all(b,0))}}var b=arguments,c=0,d=b.length,e=d,g=d<=1&&a&&f.isFunction(a.promise)... 

它不喜歡這行(第11行):

$.each(json.country ,function(){ 

得到國家的正確方法是什麼?謝謝。

+1

我不認爲json.country是去工作,因爲該數組的JSON更深一層。 – Ansari

+0

謝謝,是的,我注意到,並試圖json..country,但沒有奏效。如何跳過空白級別? – user578582

+1

克勞迪奧的答案可能有效:) – Ansari

回答

4

沒有測試過,但這樣的事情應該做的伎倆

function getWorldBankData(json){ 
    var item; 
    var itemArray = json[1]; 
    for (var i in itemArray) { 
     item = itemArray[i]; 
     $('#category').append("<option>"+ item.country.value+"</option>"); 
    }; 
} 
+0

非常好,非常感謝克勞迪奧! – user578582