世界银行JSONP解析

时间:2012-05-16 18:41:16

标签: javascript jquery 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 个答案:

答案 0 :(得分: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>");
    };
}