John
John

Reputation: 3

Weather Underground API Forecast

I am simply taking the Weather Underground API, using json and parsing variables. I have an issue. If I repeat the code or logic with a different index Json takes the last entry. I am trying to repeat the same entry but with a different index or period. The period is the different day. How would I get two responses that say:

Weather forecast for Monday is A few clouds from time to time. Low -9C. Winds NNW at 10 to 15 km/h.

Weather forecast for Wendesday is A few clouds from time to time. Low -9C. Winds NNW at 10 to 15 km/h.

Here is my code:

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

<!--Use your own API key and city location-->
<!--1.Embed the WU 3-day forecast summary feature.-->
$.ajax({
    url: "http://api.wunderground.com/api/72df18b7f213607b/forecast/q/CO/Alamosa.json",
dataType : "jsonp",
 success : function(parsed_json) {
    var forecast = parsed_json['forecast']['txt_forecast']['forecastday'];

    for (index in forecast) {

    $(".three").html('Weather forecast for '+forecast[index]['title']+' is '+forecast[index]['fcttext_metric']);


        } 
    var forecast = parsed_json['forecast']['txt_forecast']['forecastday'];

    for (index in forecast) {

    $(".three").html('Weather forecast for '+forecast[2]['title']+' is '+forecast[index]['fcttext_metric']);


        } 

    } 


}); 


});  //Closes Doc Ready Function

Here is the Weather Underground documentation http://www.wunderground.com/weather/api/d/docs?d=data/forecast&MR=1

Upvotes: 0

Views: 1899

Answers (1)

Anton Boritskiy
Anton Boritskiy

Reputation: 1569

Probably you want something like this

$(document).ready(function($){
    $.ajax({
        url: "http://api.wunderground.com/api/72df18b7f213607b/forecast/q/CO/Alamosa.json",
        dataType : "jsonp",
        success : function(parsed_json) {
            var forecast = parsed_json['forecast']['txt_forecast']['forecastday'];

            for (index in forecast) {
                var newForecastString = 'Weather forecast for ' + forecast[index]['title'] + ' is ' + forecast[index]['fcttext_metric'];
                var newForecastParagraph = $('<p/>').text(newForecastString);
                $(".three").append(newForecastParagraph);
            } 
        }
    }); 
}); 

Upvotes: 1

Related Questions