我正试图从一个研究项目的Wunderground API获取当天特定时段的每小时预报温度:
这是JSON的一个例子: http://api.wunderground.com/api/[key]/geolookup/astronomy/forecast/history_/hourly/conditions/q/mo/columbia.json
具体部分如下所示:
"hourly_forecast": [
{
"FCTTIME": {
"hour": "17","hour_padded": "17","min": "00","sec": "0","year": "2013","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "15","mday_padded": "15","yday": "195","isdst": "1","epoch": "1373925600","pretty": "5:00 PM CDT on July 15, 2013","civil": "5:00 PM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Monday","weekday_name_night": "Monday Night","weekday_name_abbrev": "Mon","weekday_name_unlang": "Monday","weekday_name_night_unlang": "Monday Night","ampm": "PM","tz": "","age": ""
},
"temp": {"english": "86", "metric": "30"},
"dewpoint": {"english": "71", "metric": "22"},
"condition": "Thunderstorm",
"icon": "tstorms",
"icon_url":"http://icons-ak.wxug.com/i/c/k/tstorms.gif",
"fctcode": "15",
"sky": "66",
"wspd": {"english": "10", "metric": "16"},
"wdir": {"dir": "ESE", "degrees": "119"},
"wx": "Scattered Thunderstorms , Scattered Light Rain Showers",
"uvi": "3",
"humidity": "60",
"windchill": {"english": "-9998", "metric": "-9998"},
"heatindex": {"english": "91", "metric": "33"},
"feelslike": {"english": "91", "metric": "33"},
"qpf": {"english": "", "metric": ""},
"snow": {"english": "", "metric": ""},
"pop": "30",
"mslp": {"english": "30.14", "metric": "1020"}
}
,
我像这样得到JSON:
$.ajax({
url : "http://api.wunderground.com/api/[key]/geolookup/forecast/hourly/history_/astronomy/conditions/q/"+lat+","+lon+".json",
dataType : "jsonp",
success : function(parsed_json) {
然后我尝试按照这样的小时预测(通常mday和小时被替换为包含今天的日期和特定小时的变量,但为了排除故障,我将这些数字放入其中)。
// get the forecast
$.each( parsed_json['hourly_forecast'], function( i, value ) {
if( value.FCTTIME.mday == 15 && value.FCTTIME.hour == 19) {
six_hour_forecast = value.temp.english;
}
然而,我一直得到错误的temp.english为six_hour_forecast。
如此接近 - 我错过了什么?
答案 0 :(得分:0)
这样做了:
// Get the forecast 6 hour temp
$.each( parsed_json['hourly_forecast'], function( index, value ) {
if( value['FCTTIME']['hour']==sunrise_hour*1 + 6 && value['FCTTIME']['mday']==window.current_day ) {
window.six_hour_forecast = value.temp.english;
}
}); // end each