2

I'm having a problem parsing "wunderground" API with JavaScript and Ajax. I can get some values and some not. Here is the link for API I use:

{
   "forecast":{
      "simpleforecast":{
         "forecastday":[
            {
               "date":{
                  "epoch":"1369429200",
                  "pretty":"11:00 PM CEST on May 24, 2013",
                  "day":24,
                  "month":5,
                  "year":2013,
                  "yday":143,
                  "hour":23,
                  "min":"00",
                  "sec":0,
                  "isdst":"1",
                  "monthname":"May",
                  "weekday_short":"Fri",
                  "weekday":"Friday",
                  "ampm":"PM",
                  "tz_short":"CEST",
                  "tz_long":"Europe/Ljubljana"
               },
               "period":1,
               "high":{
                  "fahrenheit":"63",
                  "celsius":"17"
               },
               "low":{
                  "fahrenheit":"45",
                  "celsius":"7"
               },
               "conditions":"Rain",
               "icon":"rain",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/rain.gif",
               "skyicon":"cloudy",
               "pop":90,
               "qpf_allday":{
                  "in":0.47,
                  "mm":11.9
               },
               "qpf_day":{
                  "in":0.38,
                  "mm":9.7
               },
               "qpf_night":{
                  "in":0.05,
                  "mm":1.3
               },
               "snow_allday":{
                  "in":0,
                  "cm":0
               },
               "snow_day":{
                  "in":0,
                  "cm":0
               },
               "snow_night":{
                  "in":0,
                  "cm":0
               },
               "maxwind":{
                  "mph":4,
                  "kph":6,
                  "dir":"West",
                  "degrees":270
               },
               "avewind":{
                  "mph":2,
                  "kph":3,
                  "dir":"SSW",
                  "degrees":204
               },
               "avehumidity":62,
               "maxhumidity":72,
               "minhumidity":57
            }
         ]
      }
   }
}

I would like to parse value 17 for celsius. So far I have this:

jQuery(document).ready(function($) {
  $.ajax({
  url :     "http://api.wunderground.com/api/XXXXXXXXXX/geolookup/conditions/forecast/q/Slovenia/Maribor.json",
  dataType : "jsonp",
  success : function(parsed_json) {
  var forecast_1 = parsed_json['forecast']['simpleforecast']['forecastday']['celsius'];
  document.getElementById("forecast_1").innerHTML = forecast_1;
  }
  });
});
</script>

I allwas get "undefined" value. Help please? Thank you

1 Answer 1

1

The property day is an array of object, so you have to select the array index first. Furthermore, it has no direct property celsius. There are, however, two celsius properties under high and low. So you have to decide, which to address.

All in all the result could look like this:

var forecast_1 = parsed_json['forecast']['simpleforecast']['forecastday'][0]['high']['celsius'];

Besides, proper formatting of the output helps to understand it. Use, e.g., http://jsonformatter.curiousconcept.com/.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.