这应该很简单,但是即使看看其他问题,我也无法解决。
我正在从以下链接中检索一些示例世界银行数据:
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(){
取得国家的正确方法是什么?谢谢。
最佳答案
还没有测试过,但是这样的事情应该可以解决
function getWorldBankData(json){
var item;
var itemArray = json[1];
for (var i in itemArray ) {
item = itemArray[i];
$('#category').append("<option>"+ item.country.value+"</option>");
};
}