接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9,其中ak是密鑰,自行去申請(qǐng)即可,便于大家測(cè)試,樓主就公布并了自己的Key,這樣可以直接獲取到數(shù)據(jù)。
獲取到的數(shù)據(jù)是這樣的:
{"error":0,"status":"success","date":"2014-10-27","results":[{"currentCity":"上海","pm25":"95","index":[{"title":"穿衣","zs":"較舒適","tipt":"穿衣指數(shù)","des":"建議著薄外套、開衫牛仔衫褲等服裝。年老體弱者應(yīng)適當(dāng)添加衣物,宜著夾克衫、薄毛衣等。"},{"title":"洗車","zs":"較適宜","tipt":"洗車指數(shù)","des":"較適宜洗車,未來一天無雨,風(fēng)力較小,擦洗一新的汽車至少能保持一天。"},{"title":"旅游","zs":"適宜","tipt":"旅游指數(shù)","des":"天氣較好,溫度適宜,但風(fēng)稍微有點(diǎn)大。這樣的天氣適宜旅游,您可以盡情地享受大自然的無限風(fēng)光。"},{"title":"感冒","zs":"較易發(fā)","tipt":"感冒指數(shù)","des":"天氣較涼,較易發(fā)生感冒,請(qǐng)適當(dāng)增加衣服。體質(zhì)較弱的朋友尤其應(yīng)該注意防護(hù)。"},{"title":"運(yùn)動(dòng)","zs":"較適宜","tipt":"運(yùn)動(dòng)指數(shù)","des":"天氣較好,但風(fēng)力較大,推薦您進(jìn)行室內(nèi)運(yùn)動(dòng),若在戶外運(yùn)動(dòng)請(qǐng)注意防風(fēng)。"},{"title":"紫外線強(qiáng)度","zs":"弱","tipt":"紫外線強(qiáng)度指數(shù)","des":"紫外線強(qiáng)度較弱,建議出門前涂擦SPF在12-15之間、PA+的防曬護(hù)膚品。"}],"weather_data":[{"date":"周一 10月27日 (實(shí)時(shí):19℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"東北風(fēng)3-4級(jí)","temperature":"21 ~ 16℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云轉(zhuǎn)陰","wind":"東風(fēng)微風(fēng)","temperature":"21 ~ 17℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"東風(fēng)微風(fēng)","temperature":"21 ~ 19℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"東南風(fēng)微風(fēng)","temperature":"23 ~ 20℃"}]}]}根據(jù)返回的Json定義出相應(yīng)的數(shù)據(jù)結(jié)構(gòu):
public class BaiduTQ { public int error { get; set; } public string status { get; set; } public string date { get; set; } public List<BaiduResult> results { get; set; } } public class BaiduResult { public string currentCity { get; set; } public string pm25 { get; set; } public List<BaiduIndex> index { get; set; } public List<BaiDuWeaterData> weather_data { get; set; } } public class BaiduIndex { public string title { get; set; } public string zs { get; set; } public string tipt { get; set; } public string des { get; set; } } public class BaiDuWeaterData { public string date { get; set; } public string dayPictureUrl { get; set; } public string nightPictureUrl { get; set; } public string weather { get; set; } public string wind { get; set; } public string temperature { get; set; } }然后直接通過Newtonsoft.Json 反序列化成即可。
既然是獲取天氣,肯定是希望獲取客戶所在城市的天氣,下一步則是需要根據(jù)用戶機(jī)器IP獲取所在城市,然后獲取該城市的天氣信息。
通過淘寶的IP庫,http://ip.taobao.com/,即可查詢指定IP所在的城市、國家、運(yùn)營商等。
有了上面的途徑,我們下一步的工作就是獲取客戶的外網(wǎng)IP,而外網(wǎng)IP,是機(jī)器連接外網(wǎng)才會(huì)有,所以樓主寫了一個(gè)頁面,部署在外網(wǎng)服務(wù)器。
相關(guān)代碼如下:
var ip = Request.UserHostAddress; using (var client = new WebClient()) { var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; client.Encoding = Encoding.UTF8; var str = client.DownloadString(url); Response.Write(str); }這樣我們就可以獲取到客戶所在城市的天氣數(shù)據(jù)了。
最近還有個(gè)小需求,獲取某某新聞數(shù)據(jù),樓主習(xí)慣性的查了下百度的相關(guān)資料,能通過Rss來獲取百度新聞數(shù)據(jù)。
接口地址:http://news.baidu.com/n?cmd=7&loc=0&name=%B1%B1%BE%A9&tn=rss
打開后,查看它的源,無非就是xml文件,我們可以將xml文件,序列化成對(duì)象,如果沒有接觸過這類知識(shí),可以看下《xml與對(duì)象的序列化和反序列化》。
根據(jù)它的源,就能輕松定義出數(shù)據(jù)結(jié)構(gòu)。
[XmlRoot("rss")] public class Rss { public Channel channel { get; set; } } [XmlRoot("channel")] public class Channel { public string title { get; set; } public BaiduImage image { get; set; } public string link { get; set; } public string description { get; set; } public string language { get; set; } public string lastBuildDate { get; set; } public string docs { get; set; } public string generator { get; set; } [XmlElement] public List<Channel_Item> item { get; set; } } public class BaiduImage { public string title { get; set; } public string link { get; set; } public string url { get; set; } } public class Channel_Item { public string title { get; set; } public string link { get; set; } public string pubDate { get; set; } public string guid { get; set; } public string source { get; set; } public string author { get; set; } public string description { get; set; } }序列化的方法很簡(jiǎn)單。
/// <summary> /// 反序列化 /// </summary> public static T Deserialize<T>(string xmlContent) { XmlSerializer xs = new XmlSerializer(typeof(T)); using (StringReader strReader = new StringReader(xmlContent)) { XmlReader xmlReader = XmlReader.Create(strReader); return (T)xs.Deserialize(xmlReader); } }
相關(guān)測(cè)試代碼如下:
一鍵下載
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注