2011-04-19 26 views
2

http://weather.yahooapis.com/forecastjson?w=2295424如何使用jQuery阿賈克斯閱讀雅虎天氣JSON數據

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

我想顯示 「預測」

+1

請顯示您到目前爲止的代碼,並提出一個明確的問題。 「預測」包含幾個要顯示的內容,而「顯示」的含義也不清楚。 – Tomalak 2011-04-19 13:30:17

回答

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替換 「天」 與無論你需要什麼財產。

+0

爲什麼console.log() – Egglabs 2011-04-20 13:02:19

+0

沒有結果console.log(data.forecast [0] .day) – Egglabs 2011-04-20 13:05:23

+0

@Srinivas Tamada >爲什麼console.log() 只是因爲我習慣了它。這意味着你可以使用alert()或$(#myElement).html(data.forecast [0] .day); 順便說一句,如果你不知道「console.log」來自哪裏,你應該試試Firefox上的「Firebug」或Opera中的「Dragonfly」。 – fedemp 2011-04-20 13:17:46

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我收到一個語法錯誤:我不知道爲什麼,代碼應該沒問題。

Mazi