zoukankan      html  css  js  c++  java
  • 一段获得天气信息的.net代码

    //天气信息来自 中国天气网 !
    //要考虑到封杀IP的问题,所以我将请求的数据保存到数据库,每5小时请求一次。

    **************************************************** 

    protected void Page_Load(object sender, EventArgs e)
        {

            string str = GetResponseStr();//获得json天气信息
            
    //对json格式的信息重新整理一下,因为我们要用.net自带的方法获得相关信息
            str = str.Remove(str.LastIndexOf("}"), 1);
            str = str.Remove(015);

            //使用JavaScriptSerializer对象来解析数据
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Dictionary<stringobject> json = (Dictionary<stringobject>)serializer.DeserializeObject(str);

            object str_city;        //-- 城市 -- city
            object str_weather;     //-- 天气 -- weather1
            object str_img_id;      //-- 天气图标编号 -- img1
            object str_temp;        //-- 温度 -- temp1
            object str_fl;          //-- 风力 --fl1
            object str_UV;          //-- 紫外线 -- index_uv
            object str_cy;          //-- 穿衣指数 -- index
            object str_xc;          //-- 洗车指数 -- index_xc
            object str_CO;          //-- 舒适指数 -- index_co
            object str_Time;        //-- 时间 -- date_y
            object str_week;        //-- 星期 -- week

            if (json.TryGetValue("city"out str_city))
            {
                Response.Write (str_city.ToString());//城市
            }
            if (json.TryGetValue("weather1"out str_weather))
            {
                //Response.Write(str_weather.ToString());//天气
                lab_Tianqi.Text = str_weather.ToString();
            }
            if (json.TryGetValue("temp1"out str_temp))
            {
                //Response.Write(str_temp.ToString());//温度
                lab_wendu.Text = str_temp.ToString();
            }
            if (json.TryGetValue("fl1"out str_fl))
            {
                //Response.Write(str_fl.ToString());//风力
                lab_Fengli.Text = str_fl.ToString();
            }
            if (json.TryGetValue("index_uv"out str_UV))
            {
                //Response.Write(str_UV.ToString());//紫外线
                lab_UV.Text = str_UV.ToString();
            }
            //if (json.TryGetValue("index", out str_cy))
            
    //{
            
    //    Response.Write(str_cy.ToString());//穿衣
            
    //}
            if (json.TryGetValue("index_xc"out str_xc))
            {
                //Response.Write(str_xc.ToString());//洗车
                lab_Xc.Text = str_xc.ToString();
            }
            if (json.TryGetValue("index_co"out str_CO))
            {
                //Response.Write(str_CO.ToString());//舒适
                lab_Co.Text = str_CO.ToString();
            }

            if (json.TryGetValue("date_y"out str_Time))
            {
               // Response.Write(str_Time.ToString());//时间
                lab_Time.Text = str_Time.ToString();
            }
            if (json.TryGetValue("week"out str_week))
            {
                //Response.Write(str_week.ToString());//星期
                lab_Week.Text = str_week.ToString();
            }

            if (json.TryGetValue("img1"out str_img_id))
            {
                //Response.Write(str_img_id.ToString());//图片编号
                img.Src = "http://m.weather.com.cn/img/b" + str_img_id + ".gif";
                img.Width = 50;
                img.Height = 50;
            }

        }

        /// <summary>
        
    /// 获得天气的信息(json格式)
        
    /// </summary>
        
    /// <returns></returns>
        private string GetResponseStr()
        {
            string str = "http://m.weather.com.cn/data/101010100.html";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();//获得回应的数据流
            
    //将数据流转成 String
            string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
            return result;
        }

  • 相关阅读:
    Trojan.DL.Agent.nxd和RootKit.Agent.yj木马清除
    Java中的格式化数值(eg:保留两位小数)
    Int16, Int32, Int64的一点感悟
    在win2003上设置asp网站
    WPF学习笔记.
    对WF工作流异常(Event on interface type for instance id cannot be delivered)的一点总结.
    创建,安装,调试 Windows Service
    灵活而又可怕的params参数数组
    (转) 输入码、区位码、国标码与机内码
    SQL Server 2008 未来将不再包含全文检索功能, 再研究此功能已经没多大意思了.
  • 原文地址:https://www.cnblogs.com/malaohu/p/2715817.html
Copyright © 2011-2022 走看看