gpt4 book ai didi

javascript - 如何在控制台中打印来自世界银行的 JSOP 数据集的值?

转载 作者:行者123 更新时间:2023-11-28 02:46:36 26 4
gpt4 key购买 nike

如何在控制台中打印控制台中 JSOP 对象的值。当我尝试使用 var countries = data.name 检索国家名称时,在控制台中打印未定义。我注意到 JSONP 对象中的标签是数字。我想知道我是否必须使用这些数字编写路线,但是当我尝试这样做时,它也不起作用。

var Getdata = function(data) {
console.log(data)

var countries = data.name;
console.log(countries)

}

var url = 'http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata';

var query_url = url;
console.log(query_url);

var script = document.createElement('script');
script.src = query_url;
document.getElementsByTagName('head')[0].appendChild(script);
</script>

最佳答案

查看数据,

[
// this is data[0]
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 264
},
// this is data[1]
[
// this is data[1][0]
{
"indicator": {
"id": "DT.DOD.PVLX.CD",
"value": "Present value of external debt (current US$)"
},
"country": {
"id": "1A",
"value": "Arab World"
},
"value": null,
"decimal": "0",
"date": "2015"
},
// data[1][1 ... 49] below here
... etc
]
]

获取国家列表

var countries = data[1].map(function(item) {
return item.country.value;
});

Of course, you're only getting a list of the first 50 countries in that result, you would need to call that API with some parameter to get the next 5 pages (you have page 1 of 6)

所以

http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata&page=2  
http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata&page=3
http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata&page=4
http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata&page=5
http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata&page=6

同样需要检索

这是一种检索所有国家/地区的方法

var Getdata = (function() {
var url = 'http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata';
var allCountries = [];
var callback;

return function Getdata(data) {
var hdr,
arr,
countries;
if (typeof data == 'function') {
// called by code
callback = data; // setup the callback
allCountries = [];
} else {
// called by JSONP type callback
hdr = data[0];
arr = data[1];

countries = arr.map(function(item) {
return item.country.value;
});
allCountries = allCountries.concat(countries);
}
if (!hdr || hdr.page < hdr.pages) {
// get the first (hdr is undefined) or next page
var script = document.createElement('script');
script.src = url + (hdr ? '&page=' + (hdr.page + 1) : '');
document.getElementsByTagName('head')[0].appendChild(script);
} else {
// done, callback the countries
callback(allCountries);
}
};
})();

刚刚的电话

Getdata(function(data) {
console.log(data);
});

关于javascript - 如何在控制台中打印来自世界银行的 JSOP 数据集的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41275026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com