Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用循环获取这些数据?_Javascript_Json_Ajax - Fatal编程技术网

Javascript 如何使用循环获取这些数据?

Javascript 如何使用循环获取这些数据?,javascript,json,ajax,Javascript,Json,Ajax,如何通过使用循环显示从json到html的所有数据来显示所有数据? 自动取款机,我可以打印其中一个数据。但是如果我使用的是数据,代码将不会显示任何数据 我想我搞乱了对象和数组的概念。 请告诉我,如何循环通过对象,如数组 谢谢 var getWeather=document.getElementById('weather'); var requestWeather=new-XMLHttpRequest(); //+'-31'+'&lon='+'150' requestWeather.open('

如何通过使用循环显示从json到html的所有数据来显示所有数据?

自动取款机,我可以打印其中一个数据。但是如果我使用的是数据,代码将不会显示任何数据

我想我搞乱了对象和数组的概念。 请告诉我,如何循环通过对象,如数组

谢谢

var getWeather=document.getElementById('weather');
var requestWeather=new-XMLHttpRequest();
//+'-31'+'&lon='+'150'
requestWeather.open('GET','https://fcc-weather-api.glitch.me/api/current?lat=-31&lon=150’;
requestWeather.onload=函数(){
var weatherData=JSON.parse(requestWeather.responseText);
控制台日志(天气数据);
getHTML(天气数据);
};
requestWeather.send();
函数getHTML(数据){
var weatherString=“”;
for(data.weather中的var i){
var x=data.weather[i].main;
weatherString+=“

”+x+“

”; //weatherString+=“”+data.current.summary+”

”; //console.log(数据[i].city); } getWeather.insertAdjacentHTML(“beforeed”,weatherString); }

获取对象的所有数据检查并执行递归循环

var getWeather=document.getElementById('weather');
var requestWeather=new-XMLHttpRequest();
//+'-31'+'&lon='+'150'
requestWeather.open('GET','https://fcc-weather-api.glitch.me/api/current?lat=-31&lon=150’;
requestWeather.onload=函数(){
var weatherData=JSON.parse(requestWeather.responseText);
//控制台日志(天气数据);
getHTML(天气数据);
};
requestWeather.send();
函数getHTML(数据){
var weatherString=“”;
用于(数据中的var i){
var x=数据[i];
if(类型(x)=“对象”){
getHTML(x);
}
否则{
weatherString+=“

”+i+”:“+x+”

”; //weatherString+=“”+data.current.summary+”

”; //console.log(数据[i].city); } } getWeather.insertAdjacentHTML(“beforeed”,weatherString); }

获取对象的所有数据检查并执行递归循环

var getWeather=document.getElementById('weather');
var requestWeather=new-XMLHttpRequest();
//+'-31'+'&lon='+'150'
requestWeather.open('GET','https://fcc-weather-api.glitch.me/api/current?lat=-31&lon=150’;
requestWeather.onload=函数(){
var weatherData=JSON.parse(requestWeather.responseText);
//控制台日志(天气数据);
getHTML(天气数据);
};
requestWeather.send();
函数getHTML(数据){
var weatherString=“”;
用于(数据中的var i){
var x=数据[i];
if(类型(x)=“对象”){
getHTML(x);
}
否则{
weatherString+=“

”+i+”:“+x+”

”; //weatherString+=“”+data.current.summary+”

”; //console.log(数据[i].city); } } getWeather.insertAdjacentHTML(“beforeed”,weatherString); }

var getWeather=document.getElementById('weather');
var requestWeather=new-XMLHttpRequest();
//+'-31'+'&lon='+'150'
requestWeather.open('GET','https://fcc-weather-api.glitch.me/api/current?lat=-31&lon=150’;
requestWeather.onload=函数(){
var weatherData=JSON.parse(requestWeather.responseText);
getHTML(天气数据);
};
requestWeather.send();
函数getHTML(数据){
var weatherString=“”;
for(data.weather中的var i){
var x=data.weather[i].main;
weatherString+=“

”+x+“

”; $.each(data.main,function(i,f){ var main=“”+i+”:“+f+”; $(main)。附加到(“#main”); }); } getWeather.insertAdjacentHTML(“beforeed”,weatherString); }

var getWeather=document.getElementById('weather');
var requestWeather=new-XMLHttpRequest();
//+'-31'+'&lon='+'150'
requestWeather.open('GET','https://fcc-weather-api.glitch.me/api/current?lat=-31&lon=150’;
requestWeather.onload=函数(){
var weatherData=JSON.parse(requestWeather.responseText);
getHTML(天气数据);
};
requestWeather.send();
函数getHTML(数据){
var weatherString=“”;
for(data.weather中的var i){
var x=data.weather[i].main;
weatherString+=“

”+x+“

”; $.each(data.main,function(i,f){ var main=“”+i+”:“+f+”; $(main)。附加到(“#main”); }); } getWeather.insertAdjacentHTML(“beforeed”,weatherString); }

使用foreach循环迭代所有对象
使用foreach循环迭代所有对象

api只返回一个元素,而不是要迭代的元素数组是的,如果我想显示每个数据,我必须手动执行吗?像一个接一个吗?嗯
.main
只存在一次,您必须像使用
一样获得其他键。main
好的~我想知道是否有其他方法可以做到这一点…如果我想要获得主阵列,这也将是一个问题。还有另一个数组调用[main]。在这种情况下,我需要再次循环。api只返回一个元素,而不是要迭代的元素数组。是的,如果我想显示每个数据,我必须手动执行吗?像一个接一个吗?嗯
.main
只存在一次,您必须像使用
一样获得其他键。main
好的~我想知道是否有其他方法可以做到这一点…如果我想要获得主阵列,这也将是一个问题。还有另一个数组调用[main]。在这种情况下,我需要再次循环。谢谢,@ewlink,这是一个很好的方法。但我在想,有没有办法访问每一个数据,比如data.weather[I].main;在你通过数据循环之后?因为最终,我想使用这个对象。例如,图标URL不需要循环如果你想得到一个值,你只需要索引和键,例如获取图标
数据。weather[0]。图标
你的代码是正确的,但是
数据。weather
只有一个对象,所以使用循环是没有效率的,除非你想得到所有
数据