首页 / C# / [C#]使用控制台获取天气预报
[C#]使用控制台获取天气预报
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[C#]使用控制台获取天气预报,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4656字,纯文字阅读大概需要7分钟。
内容图文
本例子主要是使用由中央气象局网站(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<Province> provinceResult = JsonConvert.DeserializeObject<List<Province>>(content); Dictionary<string, string> proviceNamedict = new Dictionary<string, 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<City> cityResult = JsonConvert.DeserializeObject<List<City>>(content); Dictionary<string, string> cityNamedict = new Dictionary<string, 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<Detail>(content); Console.WriteLine(newstring(‘-‘, 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(newstring(‘-‘, 50)); } catch(WebException ex) { Console.WriteLine(ex.Message); } Console.WriteLine("按任何键退出..."); Console.ReadKey(); } } class Province { publicstring code { set; get; } publicstring name { set; get; } publicstring url { set; get; } } class City { publicstring url { set; get; } publicstring code { set; get; } publicstring city { set; get; } publicstring province { set; get; } } class Detail { public City station { set; get; } publicstring publish_time { set; get; } public Weather weather { set; get; } public Wind wind { set; get; } public Warn warn { set; get; } } class Weather { publicfloat temperature { set; get; } publicfloat temperatureDiff { set; get; } publicfloat airpressure { set; get; } publicfloat humidity { set; get; } publicfloat rain { set; get; } publicfloat rcomfort { set; get; } publicfloat icomfort { set; get; } publicstring info { set; get; } publicstring img { set; get; } publicfloat feelst { set; get; } } class Wind { publicstring direct { set; get; } publicstring power { set; get; } publicfloat speed { set; get; } } class Warn { publicstring alert { set; get; } publicstring pic { set; get; } publicstring province { set; get; } publicstring city { set; get; } publicstring url { set; get; } publicstring issuecontent { set; get; } publicstring fmeans { set; get; } } }
6、运行效果如下:
7、源代码与可执行应用程序如下:
可执行应用程序:https://pan.baidu.com/s/1i6mK8xn
原文:https://www.cnblogs.com/cncc/p/8342884.html
内容总结
以上是互联网集市为您收集整理的[C#]使用控制台获取天气预报全部内容,希望文章能够帮你解决[C#]使用控制台获取天气预报所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。