通过使用jQuery的JSON循环
问题描述:
我想通过第一次循环json对象,我想要做的就是列出所有的temp变量的内容,但我无法计算如何循环工作。通过使用jQuery的JSON循环
$("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>');
});
});
});
答
你应该真的console.log你的对象,并找出结构。
该对象在您认为的位置没有res.wind.speed
。
这里是您的对象的概览 - >http://jsfiddle.net/t2KMk/1/
通知,list
是不同时间包含这样的事情,风,云,雨,主等
这里有一个如何遍历这些值,得到了风
$("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
});
});
});
为例
'temp'是值,不需要括号'[]' –
你试图确定你的$ .each回调期间发生了什么? –