Javascript 如何在控制台中打印来自世界银行JSOP数据集的值?
如何在控制台中打印控制台中JSOP对象的值。当我试图用var countries=data检索国家名称时,控制台中的name打印未定义。我注意到JSONP对象中的标记是数字。我想知道我是否必须用这些数字来写一条路线,但当我尝试这样做时,它也不起作用Javascript 如何在控制台中打印来自世界银行JSOP数据集的值?,javascript,html,xmlhttprequest,Javascript,Html,Xmlhttprequest,如何在控制台中打印控制台中JSOP对象的值。当我试图用var countries=data检索国家名称时,控制台中的name打印未定义。我注意到JSONP对象中的标记是数字。我想知道我是否必须用这些数字来写一条路线,但当我尝试这样做时,它也不起作用 var Getdata = function(data) { console.log(data) var countries = data.name; console.log(countries) } var url = 'http://api
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>
var Getdata=函数(数据){
console.log(数据)
var countries=data.name;
console.log(国家/地区)
}
var url='1〕http://api.worldbank.org/countries/all/indicators/DT.DOD.PVLX.CD?date=2015&format=jsonp&prefix=Getdata';
var query_url=url;
console.log(查询url);
var script=document.createElement('script');
script.src=查询url;
document.getElementsByTagName('head')[0].appendChild(脚本);
查看数据时
[
// 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;
});
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);
}
};
})();
当然,您只会得到该结果中前50个国家的列表,您需要使用一些参数调用该API以获得接下来的5页(您有第1页,共6页)
所以
也需要检索
这里有一种方法可以检索所有国家
var countries = data[1].map(function(item) {
return item.country.value;
});
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);
}
};
})();
在回调函数var Getdata=function(data)中{
-数据是一个javascript数组,不是你称之为JSONP数据集的东西
…只是一个普通的ol'javascript数组,你可以像其他javascript数组一样操作它以进行更正。我现在的问题是如何操作它。我在控制台中获得了对象,但我尝试构建一个变量:如果你查看数据,你可以get,它是一个数组,在数组中有一个对象在位置0,一个对象数组在位置1…没有一个对象有一个名为name
的属性我试图构建一个变量-好的数据有数据…如果你想让它成为一个全局变量,你可以说window.someGlobal=data
>在回调中感谢您的更正。我现在的问题是如何操作它。我在控制台中获得了对象,但我尝试构建一个变量:var countries=data.name;console.log(countries)
但是在控制台中打印的是未定义的,我如何才能真正获得国家的名称。谢谢,这对我来说真的很有用。非常感谢。@CésarBarboza-我已经添加了代码,可以读取接下来的5页,然后调用提供的回调,并返回所有6页的结果