如何使用Jquery ajax读取yahoo天气JSON数据

时间:2011-04-19 13:26:32

标签: jquery ajax json

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":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"}
]}

我想显示“预测”

2 个答案:

答案 0 :(得分:3)

$.ajax({
   url: "http://weather.yahooapis.com/forecastjson?w=2295424",
   dataType: "json",
   success: function(data) {
      console.log( data.forecast[0].day );
      }
 });

在data.forecast [0] .day中,用你需要的任何属性替换“day”。

答案 1 :(得分:0)

这应该是有效的:

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");

我尝试了自己,但是使用jQuery 1.5我收到了一个解析错误:我不知道为什么,代码应该没问题。

麻子