需要使用此JSON文档并使用$ .ajax()从API创建每小时天气更新

时间:2015-12-12 05:16:16

标签: javascript jquery json ajax api

我必须显示当天的每小时预测。 看起来像:

9p - 78F 10p - 76F 等...

这是我到目前为止所做的,但如果你看看JSON,布局很奇怪,不要让我把信息拉出来。我需要时间和温度来显示。

$.ajax({
                url: "http://api.wunderground.com/api/56763c83b2ae28c3/forecast/q/TX/Dallas.json",
                dataType: "json",
                success:function(data){
                    var hourly_forecast = data['hourly_forecast','','FCTTIME'];
                    var hour1 = hourly_forecast['hour'];
                    $('.hour1').html(hour1);
                }
            });

JSON数据(从网站复制)

{
response: {
version: "0.1",
termsofService: "http://www.wunderground.com/weather/api/d/terms.html",
features: {
hourly: 1
}
},
hourly_forecast: [
{
FCTTIME: {
hour: "15",
hour_padded: "15",
min: "00",
min_unpadded: "0",
sec: "0",
year: "2015",
mon: "12",
mon_padded: "12",
mon_abbrev: "Dec",
mday: "11",
mday_padded: "11",
yday: "344",
isdst: "0",
epoch: "1449867600",
pretty: "3:00 PM CST on December 11, 2015",
civil: "3:00 PM",
month_name: "December",
month_name_abbrev: "Dec",
weekday_name: "Friday",
weekday_name_night: "Friday Night",
weekday_name_abbrev: "Fri",
weekday_name_unlang: "Friday",
weekday_name_night_unlang: "Friday Night",
ampm: "PM",
tz: "",
age: "",
UTCDATE: ""
},
temp: {
english: "75",
metric: "24"
},
dewpoint: {
english: "61",
metric: "16"
},
condition: "Partly Cloudy",
icon: "partlycloudy",
icon_url: "http://icons.wxug.com/i/c/k/partlycloudy.gif",
fctcode: "2",
sky: "47",
wspd: {
english: "16",
metric: "26"
},
wdir: {
dir: "S",
degrees: "176"
},
wx: "Partly Cloudy",
uvi: "1",
humidity: "62",
windchill: {
english: "-9999",
metric: "-9999"
},
heatindex: {
english: "-9999",
metric: "-9999"
},
feelslike: {
english: "75",
metric: "24"
},
qpf: {
english: "0.0",
metric: "0"
},
snow: {
english: "0.0",
metric: "0"
},
pop: "0",
mslp: {
english: "29.72",
metric: "1006"
}
},

1 个答案:

答案 0 :(得分:0)

根据您提供的json回复,

更改

$.ajax({
  url: "http://api.wunderground.com/api/56763c83b2ae28c3/forecast/q/TX/Dallas.json",
  dataType: "json",
  success: function(data) {
    var hourly_forecast = data['hourly_forecast', '', 'FCTTIME'];
    var hour1 = hourly_forecast['hour'];
    $('.hour1').html(hour1);
  }
});

$.ajax({
  url: "http://api.wunderground.com/api/56763c83b2ae28c3/forecast/q/TX/Dallas.json",
  dataType: "json",
  success: function(data) {
    var hourly_forecast = data['hourly_forecast'][0]['FCTTIME'];
    var hour1 = hourly_forecast['hour'];
    $('.hour1').html(hour1);
  }
});