C#完成解析百度氣象數據,Rss解析百度消息和依據IP獲得地點城市的辦法。本站提示廣大學習愛好者:(C#完成解析百度氣象數據,Rss解析百度消息和依據IP獲得地點城市的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成解析百度氣象數據,Rss解析百度消息和依據IP獲得地點城市的辦法正文
本文實例講述了C#完成解析百度氣象數據,Rss解析百度消息和依據IP獲得地點城市的辦法,分享給年夜家供年夜家參考。詳細完成辦法以下:
1、百度氣象
接口地址:http://api.map.百度.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9,個中ak是密鑰,自行去請求便可,便於年夜家測試,樓主就頒布並了本身的Key,如許可以直接獲得到數據。
獲得到的數據是如許的:
{"error":0,"status":"success","date":"2014-10-27","results":[{"currentCity":"上海","pm25":"95","index":[{"title":"穿衣","zs":"較溫馨","tipt":"穿衣指數","des":"建議著薄外衣、開衫牛仔衫褲等服裝網www.vhao.net。年邁體弱者應恰當添加衣物,宜著茄克衫、薄毛衣等。"},{"title":"洗車","zs":"較合適","tipt":"洗車指數","des":"較合適洗車,將來一天無雨,風力較小,擦洗一新的汽車至多能堅持一天。"},{"title":"旅游","zs":"合適","tipt":"旅游指數","des":"氣象較好,溫度合適,但風略微有點年夜。如許的氣象合適旅游,您可以縱情地享用年夜天然的無窮風景。"},{"title":"傷風","zs":"較易發","tipt":"傷風指數","des":"氣象較涼,較易產生傷風,請恰當增長衣服。體質較弱的同伙特別應當留意防護。"},{"title":"活動","zs":"較合適","tipt":"活動指數","des":"氣象較好,但風力較年夜,推舉您停止室內活動,若在戶外活動請留意防風。"},{"title":"紫外線強度","zs":"弱","tipt":"紫外線強度指數","des":"紫外線強度較弱,建議出門前塗擦SPF在12-15之間、PA+的防曬護膚品。"}],"weather_data":[{"date":"周一 10月27日 (及時:19℃)","dayPictureUrl":"http://api.map.百度.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/duoyun.png","weather":"多雲","wind":"西南風3-4級","temperature":"21 ~ 16℃"},{"date":"周二","dayPictureUrl":"http://api.map.百度.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/yin.png","weather":"多雲轉陰","wind":"春風輕風","temperature":"21 ~ 17℃"},{"date":"周三","dayPictureUrl":"http://api.map.百度.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/xiaoyu.png","weather":"細雨","wind":"春風輕風","temperature":"21 ~ 19℃"},{"date":"周四","dayPictureUrl":"http://api.map.百度.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/xiaoyu.png","weather":"細雨","wind":"西北風輕風","temperature":"23 ~ 20℃"}]}]}
依據前往的Json界說出響應的數據構造:
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 反序列化成便可。
既然是獲得氣象,確定是願望獲得客戶地點城市的氣象,下一步則是須要依據用戶機械IP獲得地點城市,然後獲得該城市的氣象信息。
2、IP獲得城市
經由過程淘寶的IP庫,http://ip.taobao.com/,便可查詢指定IP地點的城市、國度、運營商等。
有了下面的門路,我們下一步的任務就是獲得客戶的外網IP,而外網IP,是機械銜接外網才會有,所以樓主寫了一個頁面,安排在外網辦事器。
相干代碼以下:
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);
}
如許我們便可以獲得到客戶地點城市的氣象數據了。
3、獲得百度消息
比來還有個小需求,獲得某某消息數據,樓主習氣性的查了下百度的相干材料,能經由過程Rss來獲得百度消息數據。
接口地址:http://news.百度.com/n?cmd=7&loc=0&name=%B1%B1%BE%A9&tn=rss
翻開後,檢查它的源,不過就是xml文件,我們可以將xml文件,序列化成對象,假如沒有接觸過這類常識,可以看下《xml與對象的序列化和反序列化》。
依據它的源,就可以輕松界說出數據構造。
[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; }
}
序列化的辦法很簡略。
/// <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);
}
}
完全實例代碼點擊此處本站下載。
願望本文所述對年夜家的C#法式設計有所贊助。