使用jquery循环JSON
我第一次尝试循环一个json对象,我只想列出temp变量的所有内容,但我不知道如何循环使用jquery循环JSON,jquery,json,loops,Jquery,Json,Loops,我第一次尝试循环一个json对象,我只想列出temp变量的所有内容,但我不知道如何循环 $("button").click(function(){ var url = "http://api.openweathermap.org/data/2.5/forecast?lat=53.7&lon=0.3&callback=?" ; $.getJSON(url, function(res) { $('#result').html('<p>lon: ' + res.
$("button").click(function(){
var url = "http://api.openweathermap.org/data/2.5/forecast?lat=53.7&lon=0.3&callback=?" ;
$.getJSON(url, function(res) {
$('#result').html('<p>lon: ' + res.city.coord.lon + '</p>');
$('#result').append('<p>lat: ' + res.city.coord.lat + '</p>');
$('#result').append('<p>wind: ' + res.wind.speed + '</p>');
$.each(res.list.main, function(i, temp) {
$('#result').append('<p>temp: ' + temp[i] + '</p>');
});
});
$(“按钮”)。单击(函数(){
变量url=”http://api.openweathermap.org/data/2.5/forecast?lat=53.7&lon=0.3&callback=?" ;
$.getJSON(url,函数(res){
$('#result').html('lon:'+res.city.coord.lon+'');
$(“#结果”).append(“lat:”+res.city.coord.lat+”);
$(“#结果”)。追加(“风:”+res.wind.speed+”);
$。每个(res.list.main,函数(i,temp){
$('#result')。追加('temp:'+temp[i]+'');
});
});
})) 你真的应该控制台。记录你的对象并弄清楚它的结构 在你认为它所在的位置,该物体中没有
res.wind.speed
以下是您的对象概述->
请注意,list
是一个不同时间等的对象数组,包含诸如风、云、雨、main等
下面是一个如何迭代这些值的示例,以获取风
$("button").click(function(){
var url = "http://api.openweathermap.org/data/2.5/forecast?lat=53.7&lon=0.3&callback=?";
$.getJSON(url, function(res) {
$('#result').html('<p>lon: ' + res.city.coord.lon + '</p>');
$('#result').append('<p>lat: ' + res.city.coord.lat + '</p>');
$.each(res.list, function(i, temp) { // iterate the array
$('#result').append('<p>wind: ' + temp.wind.speed + '</p>');
// ^^^ here you have wind
});
});
});
$(“按钮”)。单击(函数(){
变量url=”http://api.openweathermap.org/data/2.5/forecast?lat=53.7&lon=0.3&callback=?";
$.getJSON(url,函数(res){
$('#result').html('lon:'+res.city.coord.lon+'');
$(“#结果”).append(“lat:”+res.city.coord.lat+”);
$.each(res.list,function(i,temp){//迭代数组
$(“#结果”)。追加(“风:”+temp.wind.speed+”);
//这里有风
});
});
});
temp
是值,不需要括号[]
您在$回调期间试图确定发生了什么。每次回调?