C# 解析百度天氣數據,Rss解析百度新聞以及根據IP獲取所在城市

百度天氣

  接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9,其中ak是密鑰,自行去申請便可,便於你們測試,樓主就公佈並了本身的Key,這樣能夠直接獲取到數據。php

  獲取到的數據是這樣的:html

{"error":0,"status":"success","date":"2014-10-27","results":[{"currentCity":"上海","pm25":"95","index":[{"title":"穿衣","zs":"較溫馨","tipt":"穿衣指數","des":"建議着薄外套、開衫牛仔衫褲等服裝。年老體弱者應適當添加衣物,宜着夾克衫、薄毛衣等。"},{"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.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多雲","wind":"東北風3-4級","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":"多雲轉陰","wind":"東風微風","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":"東風微風","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":"東南風微風","temperature":"23 ~ 20℃"}]}]}

  根據返回的Json定義出相應的數據結構: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 反序列化成便可。api

  既然是獲取天氣,確定是但願獲取客戶所在城市的天氣,下一步則是須要根據用戶機器IP獲取所在城市,而後獲取該城市的天氣信息。服務器

IP獲取城市

  經過淘寶的IP庫,http://ip.taobao.com/,便可查詢指定IP所在的城市、國家、運營商等。數據結構

  有了上面的途徑,咱們下一步的工做就是獲取客戶的外網IP,而外網IP,是機器鏈接外網纔會有,因此樓主寫了一個頁面,部署在外網服務器。測試

  相關代碼以下:ui

         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);
            }

  這樣咱們就能夠獲取到客戶所在城市的天氣數據了。url

獲取百度新聞

  最近還有個小需求,獲取某某新聞數據,樓主習慣性的查了下百度的相關資料,能經過Rss來獲取百度新聞數據。spa

  接口地址:http://news.baidu.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);
            }
        }

  

相關測試代碼以下:

  一鍵下載

相關文章
相關標籤/搜索