我想從wunderground獲取天氣預報和條件,但是當我運行代碼時,current_observation發生錯誤,api的邊數據json中的current_observation。未捕獲TypeError:無法讀取未定義的屬性'current_observation'
$(document).ready(function($){
$.ajax({
url: "http://api.wunderground.com/api/72df18b7f213607b/conditions/forecast/lang:AR/q/CO/zmw:00000.1.WKQTZ.json",
dataType : "jsonp",
success : function(parsed_json) {
var forecast = parsed_json['forecast']['txt_forecast']['forecastday']['conditions']['current_observation'];
for (index in forecast,conditions) {
var newForecastString = 'Weather forecast for ' + forecast[index]['title'] + ' is ' + forecast[index]['fcttext_metric'];
var newconditionsString = 'Weather forecast for ' + forecast[index]['title'] + ' is ' + forecast[index]['station_id'];
var newForecastParagraph = $('<p/>').text(newForecastString);
$("body").append(newForecastParagraph);
}
}
}); });
'for(index in forecast,conditions){'Whyt is',conditions' there?這使得它忽略'forecast'並使用'conditions',這在你的例子中沒有定義。 – 2015-09-05 16:43:37
在json數據中我們有太多的對象,預測和條件 – aligassan
@aligassan這不是JSON的工作方式。頂層包含'response','current_observation'和'forecast'鍵。你不能一次循環兩件事;這個不成立。 –