C#WInFrom获取天气预报

本程序是请求国家气象局天气预报接口,拿到Json再转成List,绑定到dataGridView。

获取秦皇岛15天天气预报
http://t.weather.sojson.com/api/weather/city/101091101

其他城市编号见https://wenku.baidu.com/view/c1c74a2d7cd184254b3535f6.html
在这里插入图片描述

查询代码

在这里插入图片描述

        private void button1_Click(object sender, EventArgs e)
        {
            string bianhao = this.textBox1.Text;
            string url = "http://t.weather.sojson.com/api/weather/city/" + bianhao;   //这个是我们的URL   
            string content = GetRemoteHtmlCode(url); //获取国家气象局天气预报接口数据

            //获取字符串"["位置
            int IndexofA = content.IndexOf("[");
            int IndexofB = content.IndexOf("]");
            //截取位数字符串
            string weatherJson = content.Substring(IndexofA, IndexofB - IndexofA + 1);
            //JSON转List
            JavaScriptSerializer weatherSerializer = new JavaScriptSerializer();
            //拿到15天天气预报List
            List<WeatherinfoModel> weatherinfoModelList = weatherSerializer.Deserialize<List<WeatherinfoModel>>(weatherJson);

            int IndexofC = content.IndexOf("{",1);
            int IndexofD = content.IndexOf("}");
            string regionJson = content.Substring(IndexofC, IndexofD - IndexofC + 1);
            //拿到城市model
            CityModel cityModel = new CityModel();
            JavaScriptSerializer citySerializer = new JavaScriptSerializer();
            cityModel = citySerializer.Deserialize<CityModel>(regionJson);
            
            //组合返回List
            weatherinfoModelList.ForEach(t => 
            {
                t.city = cityModel.city;
                t.citykey = cityModel.citykey;
                t.parent = cityModel.parent;
                t.updateTime = DateTime.Today.ToString("yyyy-MM-dd") + " " + cityModel.updateTime;
            });
			//将list绑定到dataGridView
            this.dataGridView1.DataSource = weatherinfoModelList;
        }

传入URL,返回URL的HTML源码

		private static string GetRemoteHtmlCode(string url)
        {
            string HtmlCode = "";
            HttpWebRequest wrequest = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                HttpWebResponse wresponse = (HttpWebResponse)wrequest.GetResponse();
                Stream stream = wresponse.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                HtmlCode = reader.ReadToEnd();
                reader.Close();
                wresponse.Close();
            }
            catch
            {
                HtmlCode = "Error";
            }
            return HtmlCode;
        }

Model

    /// <summary>
    /// 天气情况实体
    /// </summary>
    public class WeatherinfoModel
    {
        /// <summary>
        /// 城市名称
        /// </summary>
        public string city { get; set; }
        /// <summary>
        /// 城市编号
        /// </summary>
        public string citykey { get; set; }
        /// <summary>
        /// 城市简称
        /// </summary>
        public string parent { get; set; }
        /// <summary>
        /// 更新时间
        /// </summary>
        public string updateTime { get; set; }
        /// <summary>
        /// 时间
        /// </summary>
        public string date { get; set; }
        /// <summary>
        /// 最高温度
        /// </summary>
        public string high { get; set; }
        /// <summary>
        /// 最低温度
        /// </summary>
        public string low { get; set; }
        /// <summary>
        /// 日期
        /// </summary>
        public string ymd { get; set; }
        /// <summary>
        /// 星期
        /// </summary>
        public string week { get; set; }
        /// <summary>
        /// 太阳升起时间
        /// </summary>
        public string sunrise { get; set; }
        /// <summary>
        /// 太阳降落时间
        /// </summary>
        public string sunset { get; set; }
        /// <summary>
        /// 空气质量
        /// </summary>
        public string aqi { get; set; }
        /// <summary>
        /// 风向
        /// </summary>
        public string fx { get; set; }
        /// <summary>
        /// 风速
        /// </summary>
        public string fl { get; set; }
        /// <summary>
        /// 天气
        /// </summary>
        public string type { get; set; }

        public string notice { get; set; }
    }

    /// <summary>
    /// 城市实体
    /// </summary>
    public class CityModel
    {
        /// <summary>
        /// 城市名称
        /// </summary>
        public string city { get; set; }
        /// <summary>
        /// 城市编号
        /// </summary>
        public string citykey { get; set; }
        /// <summary>
        /// 城市简称
        /// </summary>
        public string parent { get; set; }
        /// <summary>
        /// 更新时间
        /// </summary>
        public string updateTime { get; set; }
    }

总结

本程序主要根据国家气象局天气预报接口获取未来十五天天气预报。只用来新手学习,里边主要涉及到Url请求,截取字符串,Json转List,Json转model。
希望能够帮助大家,本人也是小白,共同进步。

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值