2

这应该是非常直接的,但即使查看其他问题,我也无法让它发挥作用。

我正在从以下链接中检索一些世界银行数据样本:

世界银行查询

这将返回以下 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(){

获得国家的正确方法是什么?谢谢。

4

1 回答 1

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>");
    };
}
于 2012-05-16T18:51:40.013 回答