本例子主要是使用由中央气象局网站(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(new string('-', ));
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('-', ));
}
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/1i6mK8xn

[C#]使用控制台获取天气预报的更多相关文章

  1. [整]C#获取天气预报信息(baidu api)包括pm2.5

    /// <summary> /// 获取天气预报信息 /// </summary> /// <returns></returns> public Bai ...

  2. java获取天气预报的信息

    运行效果: 主要功能: 1,jsp页面输入省份和城市 根据条件获取当地的天气信息 2,java代码 利用第三方的省份和城市的路径地址 本工程主要实现java获取天气预报的信息步骤1,创建工程weath ...

  3. Android 获取天气预报

    界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  4. php从气象局获取天气预报并保存到服务器

    思路:1.打开网页时读取中国气象网的接口得到每个城市的该日json:2.解析并保存到mysql:3.客户端访问mysql得到数据集. 所包含的技巧: 进度条.flush()问题.mysql.xml.p ...

  5. springboot 采用HttpClient获取天气预报 异常及原因

    采用httpClient调用天气预报地址获取出现异常 2018-10-04 15:18:25.815 ERROR 10868 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[ ...

  6. Ajax的封装,以及利用jquery的ajax获取天气预报

    1.Ajax的封装 function ajax(type,url,param,sync,datetype,callback){//第一个参数是获取数据的类型,第二个参数是传入open的url,第三个是 ...

  7. 获取天气预报API5_统计最容易生病时间段

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  8. 获取天气预报API

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  9. 跨域使用jsonp 获取天气预报

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">    ...

随机推荐

  1. 用原生实现点击删除点击的li

    简单的实现方式 <!DOCTYPE html> <html> <head> <title></title> <meta charset ...

  2. MySQL 单实例编译安装 以及多实例安装简介

    这是基本的安装教程,与牛逼的大神无关,或许是牛逼大神不用看就会安装吧. CentOS 6.5 Final  x86_64 一.预安装软件包 1.开发包组合安装 yum groupinstall &qu ...

  3. K:java中正则表达式的使用说明及其举例

    从Java1.4起,java核心API就引入了java.util.regex程序包来处理正则表达式,并使用该包下的相关类进行字符串的匹配.搜索.提取.分析结构化内容等工作.需要注意的是,正则表达式本身 ...

  4. ABP .Net Core 日志组件集成使用NLog

    一.说明 NLog介绍和使用说明官网:http://nlog-project.org/ NLog和Log4net对比:https://www.cnblogs.com/qinjin/p/5134982. ...

  5. 导出生成xsl文件

    public String expData() throws Exception{              List<SubArea> list = subAreaService.fin ...

  6. php接口interface的使用

    接口是什么? 使用接口(interface),可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容. 接口是通过 interface 关键字来定义的,就像定义一个标准的类一样,但其中定义所有 ...

  7. 总结基础OOP(面向对象)

    OOP其实也就是面向对象编程.  一:什么是对象:  我们最常见的理解方式无非是:对象(object)是任何看得见.摸得着.感觉得到,可以获得的东西,有自己的标识的任何东西.对象是某一类的事物的具体个 ...

  8. Jerry的UI5框架代码自学教程

    SAP UI5对View元素基于jQuery的操作方式,使得其学习曲线相对Angular/React来说比较平缓,至少对于我个人而言是这样.对于已经有jQuery经验的前端开发人员来说很容易上手. 使 ...

  9. 入门干货之Grpc的.Net实现-MagicOnion

    此文章简单残暴,学习成本较低,你可以跟着我一起撸代码,一起吐槽,一起砸键盘.以下操作均为 core2.0 环境. 0x01.Grpc 1.介绍  Google主导开发的RPC框架,使用HTTP/2协议 ...

  10. JavaScript(六)函数

    函数的声明方式 function name () {}  函数声明 var name = function(){}  函数表达式 所有函数都有返回值  未return 的函数  返回值 是  unde ...