[C#]使用控制台获取天气预报

2021-02-18 11:21

阅读:631

本例子主要是使用由中央气象局网站(http://www.nmc.gov.cn)提供的JSON API,其实现思路如下:

1、访问获取省份(包含直辖市、自治区等,以下简称省份)的网址(http://www.nmc.gov.cn/f/rest/province),返回对应的省份名称(name)、代码(code)等,如下图所示:

技术分享图片

2、根据以上返回的代码(code),将代码拼接在网址(http://www.nmc.gov.cn/f/rest/province)的后面,如返回的代码为 AGD (广东省),则拼接后的网址为http://www.nmc.gov.cn/f/rest/province/AGD,以此获得对应的城市名称(city)、代码(code),如下图所示:

 技术分享图片

3、根据以上返回的代码,将代码拼接在网址(http://www.nmc.gov.cn/f/rest/real/)的后面,如返回的代码为 59287 (广州),则拼接后的网址为http://www.nmc.gov.cn/f/rest/real/59287,以此获得对应的天气信息,如下图所示:

 技术分享图片

4、本例子使用的技术为 HttpWebRequest类、HttpWebResponse类及Newtonsoft.Json.JsonConvert类的使用,自己有不懂的,请自行进行百度;

5、源代码如下:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace Weather
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest request = WebRequest.CreateHttp(@"http://www.nmc.gov.cn/f/rest/province");
            try
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                string content = reader.ReadToEnd();
                List provinceResult = JsonConvert.DeserializeObject>(content);
                Dictionarystring, string> proviceNamedict = new Dictionarystring, string>();
                Console.WriteLine("省及直辖市:");
                provinceResult.ForEach(x =>
                {
                    proviceNamedict.Add(x.name, x.code);
                    Console.WriteLine(x.name);
                });
                string provice;
                while (true)
                {
                    Console.Write("请输入需要查询的省或直辖市:");
                    provice = Console.ReadLine();
                    if (proviceNamedict.Keys.Contains(provice)) break;
                }
                Console.Clear();
                request = WebRequest.CreateHttp($"http://www.nmc.gov.cn/f/rest/province/{proviceNamedict[provice]}");
                response = request.GetResponse() as HttpWebResponse;
                stream = response.GetResponseStream();
                reader = new StreamReader(stream);
                content = reader.ReadToEnd();
                List cityResult = JsonConvert.DeserializeObject>(content);
                Dictionarystring, string> cityNamedict = new Dictionarystring, string>();
                Console.WriteLine("城市:");
                cityResult.ForEach(x =>
                {
                    cityNamedict.Add(x.city, x.code);
                    Console.WriteLine(x.city);
                });
                string city;
                while (true)
                {
                    Console.Write("请输入需要查询的城市:");
                    city = Console.ReadLine();
                    if (cityNamedict.Keys.Contains(city)) break;
                }
                request = WebRequest.CreateHttp($"http://www.nmc.gov.cn/f/rest/real/{cityNamedict[city]}");
                response = request.GetResponse() as HttpWebResponse;
                stream = response.GetResponseStream();
                reader = new StreamReader(stream);
                content = reader.ReadToEnd();
                Detail detailResult = JsonConvert.DeserializeObject(content);
                Console.WriteLine(new string(-, 50));
                Console.WriteLine("详细情况如下:");
                Console.WriteLine($"{detailResult.station.province},{detailResult.station.city} 发布时间:{detailResult.publish_time}");
                Console.WriteLine($"温度:{detailResult.weather.temperature}℃ 温差:{detailResult.weather.temperatureDiff}℃ 气压:{detailResult.weather.airpressure}hPa 湿度:{detailResult.weather.humidity}% 雨量:{detailResult.weather.rain}mm");
                Console.WriteLine($"天气状况:{detailResult.weather.info}");
                Console.WriteLine($"风向:{detailResult.wind.direct} {detailResult.wind.power} 风速:{detailResult.wind.speed}m/s");
                Console.WriteLine(new string(-, 50));
            }
            catch(WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("按任何键退出...");
            Console.ReadKey();
        }
    }

    class Province
    {
        public string code { set; get; }
        public string name { set; get; }
        public string url { set; get; }
    }

    class City
    {
        public string url { set; get; }
        public string code { set; get; }
        public string city { set; get; }
        public string province { set; get; }
    }

    class Detail
    {
        public City station { set; get; }
        public string publish_time { set; get; }
        public Weather weather { set; get; }
        public Wind wind { set; get; }
        public Warn warn { set; get; }
    }

    class Weather
    {
        public float temperature { set; get; }
        public float temperatureDiff { set; get; }
        public float airpressure { set; get; }
        public float humidity { set; get; }
        public float rain { set; get; }
        public float rcomfort { set; get; }
        public float icomfort { set; get; }
        public string info { set; get; }
        public string img { set; get; }
        public float feelst { set; get; }
    }

    class Wind
    {
        public string direct { set; get; }
        public string power { set; get; }
        public float speed { set; get; }
    }
    class Warn
    {
        public string alert { set; get; }
        public string pic { set; get; }
        public string province { set; get; }
        public string city { set; get; }
        public string url { set; get; }
        public string issuecontent { set; get; }
        public string fmeans { set; get; }
    }
}

6、运行效果如下:

技术分享图片

7、源代码与可执行应用程序如下:

源代码:https://pan.baidu.com/s/1pM98VnP
可执行应用程序:https://pan.baidu.com/s/1i6mK8xn

 


评论


亲,登录后才可以留言!