How to read yahoo weather JSON data with Jquery ajax
http://weather.yahooapis.com/forecastjson?w=2295424
{
"units":
{"temperature":"F","speed":"mph","distance":"mi","pressure":"in"},
"location":{"location_id":"INXX0075","city":"Madras","state_abbreviation":"*","country_abbreviation":"IN","elevation":49,"latitude":13,"longitude":80.18000000000001},
"wind":{"speed":12.00000000000000,"direction":"E"},
"atmosphere":{"humidity":"23","visibility":"4.35","pressure":"29.77","rising":"steady"},
"url":"http:\/\/weather.yahoo.com\/forecast\/INXX0075.html","logo":"http:\/\/l.yimg.com\/a\/i\/us\/nt\/ma\/ma_nws-we_1.gif","astronomy":{"sunrise":"06:20","sunset":"18:19"},"condition":{"text":"Sunny","code":"32","image":"http:\/\/l.yimg.com\/a\/i\/us\/we\/52\/32.gif","temperature":开发者_JAVA百科93.00000000000000},
"forecast":[{
"day":"Today","condition":"Mostly Clear","high_temperature":"91","low_temperature":"69"},{"day":"Tomorrow","condition":"Partly Cloudy","high_temperature":"90","low_temperature":"70"}
]}
I want to display "forecast"
$.ajax({
url: "http://weather.yahooapis.com/forecastjson?w=2295424",
dataType: "json",
success: function(data) {
console.log( data.forecast[0].day );
}
});
In data.forecast[0].day you replace "day" with whatever property you need.
This should be work:
console.debug("initYahooWeather starting");
var urlYahooWeather = "http://weather.yahooapis.com/forecastjson?w=2295424";
var urlFlickr = "http://weather.yahooapis.com/forecastjson?jsoncallback=?&w=2295424";
jQuery.ajax({
async: false,
type: "POST",
contentType: "application/json",
dataType: "json",
url: urlFlickr,
success: function(data){
console.dir(data);
console.dir(data.forecast);
// here you have to navigate the array
},
error: function(msg){
console.debug("error contacting JSON server side component...");
console.debug(msg);
}
});
console.debug("initYahooWeather stop");
I tried myself but with jQuery 1.5 I receive a parse error: I dunno why, the code should be ok.
Mazi
精彩评论