需要帮助,了解如何使用Json将Wunderground API预测的响应解析3天

时间:2014-02-26 04:13:31

标签: javascript jquery json api wunderground

我成功地解析了Wunderground API的响应并显示了对州和某个城市的“当前”天气状况的响应。这告诉我通话时外面的准确温度和当前条件。

我现在需要这种能力,而且我无法获得3天的预测响应以及我当前的天气状况响应。

我的最终结果我想要一个3天的预测结果,并且所有那些日子只包含那些日子的时间。

示例:

  • 第01天 - 周一高/低
  • 第02天 - 周二高/低
  • 第3天 - 周三高/低

第01天技术上是当天。

如果有人现在可以查看我的脚本,请帮助添加遗漏的内容我会很感激。

这是我的JS

    jQuery(document).ready(function($) {

      /* Edit these variables */
      var api = "2ea138a9dd52eabe";
      var state = "TX";
      var city = "Dallas";

      $.ajax({
      url: "http://api.wunderground.com/api/" + api + "/forecast/conditions/q/" + state + "/" + city + ".json",
      dataType : "jsonp",
      success : function(parsed_json) {
          var icon_url_json = "http://icons.wxug.com/i/c/f/" + parsed_json['current_observation']['icon'] + ".gif";
          var icon_json = '<img src ="' + icon_url_json + '" />';
          var temp_json = parsed_json['current_observation']['temp_f'];
            temp_json += "<span>°F</span>";
          var condition_json = parsed_json['current_observation']['weather'];
          var real_feel_json = "Feels Like " + parsed_json['current_observation']['feelslike_f'] + "°F";
          var wind_json = 'Winds are ' + parsed_json['current_observation']['wind_string'];
          var location_json = city + ', ' + state;


          var forecastArray = parsed_json['forecast']['txt_forecast']['forecastday'];
                if (forecastArray !== null) {

                    for (var i = 0; i < forecastArray.length; i++) {

                        if (forecastArray[i] !== null) {
                            //use can use this to assing directly to HTML element    
                            console.log(forecastArray[i]['period']);
                            console.log(forecastArray[i]['title']);
                            console.log(forecastArray[i]['fcttext']);

                        }

                    }}

      document.getElementById("weather-icon").innerHTML = icon_json;
      document.getElementById("temp").innerHTML = temp_json;
      document.getElementById("condition").innerHTML = condition_json;  
      document.getElementById("real-feel").innerHTML = real_feel_json;
      document.getElementById("wind").innerHTML = wind_json;
      document.getElementById("location").innerHTML = location_json;

      }
      });
    });

我也把它放在这里供人们看看它现在正在工作。 http://jsfiddle.net/supasmo/9d5bh/2/

1 个答案:

答案 0 :(得分:-1)

您实际上可以在接下来的10天内获取http://jsfiddle.net/xxk7S/已更新。这是更新的代码

 for(var some in parsed_json.forecast.txt_forecast.forecastday){
             console.log("*************");
          $("#nxtDays").append("<b>"+parsed_json.forecast.txt_forecast.forecastday[some].title+" </b> <br />"+
 parsed_json.forecast.txt_forecast.forecastday[some].fcttext+"<br />");                              console.log(parsed_json.forecast.txt_forecast.forecastday[some].title);
      }