jquery和雅虎的yql服務(wù)實(shí)現(xiàn)天氣預(yù)報(bào)服務(wù)示例
本代碼不涉及任何后端開發(fā)代碼(如.Net,JAVA等)。目前最權(quán)威的天氣預(yù)報(bào)數(shù)據(jù)是中國(guó)天氣網(wǎng)(http://www.weather.com.cn/),因?yàn)檫@個(gè)是官方提供的氣象數(shù)據(jù),除了商業(yè)的增值服務(wù)外,還提供了免費(fèi)的以JSON數(shù)據(jù)格式返回的氣象數(shù)據(jù),以查看杭州的天氣數(shù)據(jù)為例,可以輸入以下地址:http://m.weather.com.cn/data/101210101.html ,返回的JSON數(shù)據(jù)格式如下圖:
YQL服務(wù)可以實(shí)現(xiàn)對(duì)網(wǎng)上不同數(shù)據(jù)源的query,filter,combine(查詢,過濾,合并),提供類似SQL,具體地址如下:http://developer.yahoo.com/yql/console/ 。當(dāng)實(shí)施查詢的時(shí)候,YQL服務(wù)就會(huì)訪問網(wǎng)絡(luò)上的數(shù)據(jù)源,傳輸數(shù)據(jù),返回XML或者JSON形式的數(shù)據(jù)結(jié)果。YQL可以使用許多類型的數(shù)據(jù)源,包括Yahoo!Web services 或者其他的網(wǎng)絡(luò)服務(wù),和網(wǎng)絡(luò)數(shù)據(jù)類型例如:HTML, XML, RSS,和Atom。
因此可以通過兩者的結(jié)合使用,完成天氣預(yù)報(bào)功能的開發(fā),具體JS代碼如下:
function getWeather() {
$.getJSON("http://query.yahooapis.com/v1/public/yql", {
q: "select * from json where url=\"http://m.weather.com.cn/data/101210101.html\"",
format: "json"
}, function (data) {
if (data.query.results) {
//$("#content").text(JSON.stringify(data.query.results));
var J_data = JSON.parse(JSON.stringify(data.query.results));
//alert(J_data.weatherinfo.city);
$("#content").append("<p>"+J_data.weatherinfo.city+"天氣預(yù)報(bào)(數(shù)據(jù)來源中國(guó)天氣網(wǎng))"+"</p>");
$("#content").append("<p>"+J_data.weatherinfo.date_y+" "+J_data.weatherinfo.week+" "+J_data.weatherinfo.temp1+" "+J_data.weatherinfo.weather1+" "+J_data.weatherinfo.wind1+" "+J_data.weatherinfo.index+" "+J_data.weatherinfo.index_d+"</p>");
var t= J_data.weatherinfo.date_y;
t=t.replace("年","/");
t=t.replace("月","/");
t=t.replace("日","");
var tdy = new Date(t);
var t2 = new Date();
t2.setDate(tdy.getDate()+1);
$("#content").append("<p>"+ t2.Format("yyyy年MM月dd日")+" "+getweekdays(t2)+" "+J_data.weatherinfo.temp2+" "+J_data.weatherinfo.weather2+" "+J_data.weatherinfo.wind2+"</p>");
var t3 = new Date();
t3.setDate(tdy.getDate()+2);
$("#content").append("<p>"+t3.Format("yyyy年MM月dd日")+" "+getweekdays(t3)+" "+J_data.weatherinfo.temp3+" "+J_data.weatherinfo.weather3+" "+J_data.weatherinfo.wind3+"</p>");
var t4 = new Date();
t4.setDate(tdy.getDate()+3);
$("#content").append("<p>"+t4.Format("yyyy年MM月dd日")+" "+getweekdays(t4)+" "+J_data.weatherinfo.temp4+" "+J_data.weatherinfo.weather4+" "+J_data.weatherinfo.wind4+"</p>");
var t5 = new Date();
t5.setDate(tdy.getDate()+4);
$("#content").append("<p>"+t5.Format("yyyy年MM月dd日")+" "+getweekdays(t5)+" "+J_data.weatherinfo.temp5+" "+J_data.weatherinfo.weather5+" "+J_data.weatherinfo.wind5+"</p>");
var t6 = new Date();
t6.setDate(tdy.getDate()+5);
$("#content").append("<p>"+t6.Format("yyyy年MM月dd日")+" "+getweekdays(t6)+" "+J_data.weatherinfo.temp6+" "+J_data.weatherinfo.weather6+" "+J_data.weatherinfo.wind6+"</p>");
//alert(getweekdays(t2));
} else {
$("#content").text('no such code: ' + code);
}
});
//$.getJSON("http://m.weather.com.cn/data/101210101.html", null, function(json) { alert(json); });
}
function getweekdays(datey)
{
if(datey.getDay()==0)
{
return "星期日";
}
else if(datey.getDay()==1)
{
return "星期一";
}
else if(datey.getDay()==2)
{
return "星期二";
}
else if(datey.getDay()==3)
{
return "星期三";
}
else if(datey.getDay()==4)
{
return "星期四";
}
else if(datey.getDay()==5)
{
return "星期五";
}
else if(datey.getDay()==6)
{
return "星期六";
}
}
最終實(shí)現(xiàn)的效果,如下圖:
- jQuery實(shí)現(xiàn)仿騰訊微博滑出效果報(bào)告每日天氣的方法
- node.js 中國(guó)天氣預(yù)報(bào) 簡(jiǎn)單實(shí)現(xiàn)
- js獲取新浪天氣接口的實(shí)現(xiàn)代碼
- JS顯示日歷和天氣的方法
- newtonsoft.json解析天氣數(shù)據(jù)出錯(cuò)解決方法
- 用javascript為頁(yè)面添加天氣顯示實(shí)現(xiàn)思路及代碼
- android調(diào)用國(guó)家氣象局天氣預(yù)報(bào)接口json數(shù)據(jù)格式解釋
- JS HTML5 音樂天氣播放器(Ajax獲取天氣信息)
- asp下以Json獲取中國(guó)天氣網(wǎng)天氣的代碼
- Jquery獲取當(dāng)前城市的天氣信息
相關(guān)文章
jQuery scroll事件實(shí)現(xiàn)監(jiān)控滾動(dòng)條分頁(yè)示例
這篇文章主要介紹了jQuery scroll事件實(shí)現(xiàn)監(jiān)控滾動(dòng)條分頁(yè)簡(jiǎn)單示例,使用ajax加載,同時(shí)介紹了(document).height()與$(window).height()的區(qū)別,需要的朋友可以參考下2014-04-04jquery實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)功能
本文主要介紹了用的jqueryRotate插件實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)功能的方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01jQuery中調(diào)用WebService方法小結(jié)
以前在寫ajax請(qǐng)求時(shí),總是喜歡使用jQuery+ashx的方式進(jìn)行調(diào)用,今天采取jQuery+WebService的方法來做ajax請(qǐng)求,發(fā)現(xiàn)這種方式比使用ashx的方式要更方便。2011-03-03JQuery實(shí)現(xiàn)表格中相同單元格合并示例代碼
一定要注意如果從list的開始元素循環(huán)下去,remove掉一個(gè)元素后,有些元素就找不到了或者說不是要找的那個(gè)元素,感興趣的各位可以研究下哈2013-06-06jQuery實(shí)現(xiàn)點(diǎn)擊按鈕文字變成input框點(diǎn)擊保存變成文字
這篇文章主要介紹了jQuery實(shí)現(xiàn)點(diǎn)擊按鈕文字變成input框點(diǎn)擊保存變成文字的相關(guān)資料,非常具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05jquery實(shí)現(xiàn)彈出div,始終顯示在屏幕正中間的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)jquery實(shí)現(xiàn)彈出div,始終顯示在屏幕正中間的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03