jquery和雅虎的yql服務(wù)實現(xiàn)天氣預(yù)報服務(wù)示例
本代碼不涉及任何后端開發(fā)代碼(如.Net,JAVA等)。目前最權(quán)威的天氣預(yù)報數(shù)據(jù)是中國天氣網(wǎng)(http://www.weather.com.cn/),因為這個是官方提供的氣象數(shù)據(jù),除了商業(yè)的增值服務(wù)外,還提供了免費的以JSON數(shù)據(jù)格式返回的氣象數(shù)據(jù),以查看杭州的天氣數(shù)據(jù)為例,可以輸入以下地址:http://m.weather.com.cn/data/101210101.html ,返回的JSON數(shù)據(jù)格式如下圖:
YQL服務(wù)可以實現(xiàn)對網(wǎng)上不同數(shù)據(jù)源的query,filter,combine(查詢,過濾,合并),提供類似SQL,具體地址如下:http://developer.yahoo.com/yql/console/ 。當(dāng)實施查詢的時候,YQL服務(wù)就會訪問網(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ù)報功能的開發(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ù)報(數(shù)據(jù)來源中國天氣網(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 "星期六";
}
}
最終實現(xiàn)的效果,如下圖:
相關(guān)文章
JQuery fileupload插件實現(xiàn)文件上傳功能
這篇文章主要介紹了JQuery fileupload插件實現(xiàn)文件上傳功能的相關(guān)資料,需要的朋友可以參考下2016-03-03
簡單實現(xiàn)兼容各大瀏覽器的js復(fù)制內(nèi)容到剪切板
相信這個功能大家平時上網(wǎng)經(jīng)常能碰到,去看了幾個常用的網(wǎng)站,都是用的透明flash遮擋“復(fù)制到剪貼板”按鈕,所以當(dāng)你點擊按鈕的時候,點擊的其實是flash,然后把你需要復(fù)制的內(nèi)容傳入到了flash,最后通過flash的復(fù)制功能把傳入的內(nèi)容復(fù)制到了剪貼板。2015-09-09
詳解jquery插件jquery.viewport.js學(xué)習(xí)使用方法
本篇文章主要介紹了詳解jquery插件jquery.viewport.js學(xué)習(xí)使用方法,具有一定的參考價值,有興趣的可以了解一下2017-09-09
jQuery插件FusionWidgets實現(xiàn)的AngularGauge圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件FusionWidgets實現(xiàn)的AngularGauge圖效果,結(jié)合具體實例形式分析了jQuery使用FusionWidgets插件載入xml數(shù)據(jù)實現(xiàn)AngularGauge圖的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
easyui簡介_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了easyui簡介,詳細(xì)的介紹了什么是easyui和easyui的用法,有興趣的可以了解一下2017-07-07

