I got a sample json url from the wunderground:
var url = "http://api.wunderground.com/api/<API_KEY>/conditions/q/CA/San_Francisco.json";
which its output json is:
{"response":{"version":"0.1","termsofService":"http://www.wunderground.com/weather/api/d/terms.html","features":{"conditions":1}},"current_observation":{"image":{"url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png","title":"Weather Underground","link":"http://www.wunderground.com"},"display_location":{"full":"San Francisco, CA","city":"San Francisco","state":"CA","state_name":"California","country":"US","country_iso3166":"US","zip":"94101","magic":"1","wmo":"99999","latitude":"37.77500916","longitude":"-122.41825867","elevation":"47.00000000"},"observation_location":{"full":"SOMA - Near Van Ness, San Francisco, California","city":"SOMA - Near Van Ness, San Francisco","state":"California","country":"US","country_iso3166":"US","latitude":"37.773285","longitude":"-122.417725","elevation":"49 ft"},"estimated":{},"station_id":"KCASANFR58","observation_time":"Last Updated on January 15, 12:12 PM PST","observation_time_rfc822":"Wed, 15 Jan 2014 12:12:06 -0800","observation_epoch":"1389816726","local_time_rfc822":"Wed, 15 Jan 2014 12:12:10 -0800","local_epoch":"1389816730","local_tz_short":"PST","local_tz_long":"America/Los_Angeles","local_tz_offset":"-0800","weather":"Partly Cloudy","temperature_string":"69.8 F (21.0 C)","temp_f":69.8,"temp_c":21.0,"relative_humidity":"28%","wind_string":"From the WNW at 2.0 MPH","wind_dir":"WNW","wind_degrees":295,"wind_mph":2.0,"wind_gust_mph":0,"wind_kph":3.2,"wind_gust_kph":0,"pressure_mb":"1026","pressure_in":"30.29","pressure_trend":"-","dewpoint_string":"35 F (2 C)","dewpoint_f":35,"dewpoint_c":2,"heat_index_string":"NA","heat_index_f":"NA","heat_index_c":"NA","windchill_string":"NA","windchill_f":"NA","windchill_c":"NA","feelslike_string":"69.8 F (21.0 C)","feelslike_f":"69.8","feelslike_c":"21.0","visibility_mi":"10.0","visibility_km":"16.1","solarradiation":"--","UV":"3","precip_1hr_string":"0.00 in ( 0 mm)","precip_1hr_in":"0.00","precip_1hr_metric":" 0","precip_today_string":"0.00 in (0 mm)","precip_today_in":"0.00","precip_today_metric":"0","icon":"partlycloudy","icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif","forecast_url":"http://www.wunderground.com/US/CA/San_Francisco.html","history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KCASANFR58","ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.773285,-122.417725"}}
so now how are we suppose to get this output!? we have 2 different solution:
1- the old solution is using ajax to get the json as a string and then parse it as an object, something like this:
GET_JSON = function(callback){
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.onreadystatechange = function(){
if(this.readyState==4){
callback(JSON.parse(req.responseText));
}
}
req.send(null);
}
JSONCallback = function(JSONObj){
alert(JSONObj);
};
GET_JSON(JSONCallback);
but the problem here is if you do that in your site you would face with this ERROR:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://mydomain.com' is therefore not allowed access.
2- No problem, wunderground weather api
supports JSONP
and it will help us to solve it, we have to just make a JSONP
call, the tricky point here is, we won't use the regular ajax solution anymore:
GET_JSON = function(callback){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = url + "?callback=" + callback;
document.body.appendChild(script);
}
window.JSONCallback = function(JSONObj){
alert(JSONObj);
};
GET_JSON("JSONCallback");
there is a very tricky point that is very important. if you check the last line instead of passing the callback object as a argument, I have passed its name, why is that?
the answer would be, if we want to have a JSONP call we have to pass the callback function's name with callback
queryString. like this:
url = "http:// ... .json?callback=JSONCallback";
this was all you need to know to have access to your json object, without you needing to parse the string.