本例子主要是使用由中央气象局网站(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. 【ASP.NET MVC系列】浅谈NuGet在VS中的运用

    一     概述 在我们讲解NuGet前,我们先来看看一个例子. 1.例子: 假设现在开发一套系统,其中前端框架我们选择Bootstrap,由于选择Bootstrap作为前端框架,因此,在项目中,我们 ...

  2. 从源码(编译)安装golang 二

    h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; te ...

  3. Python安装和开发环境搭建

    1.官网:http://www.python.org/download/下载安装包,目前最新版本为3.6,安装包很多地方可以下,也可以在360软件管家上下载安装  特别要注意勾选:Add Python ...

  4. 当你的电脑出现stop: 0X0000007B

    这几天可算是把我折腾惨了.先是linux系统无法进入图形化桌面,几经折腾,我把linux删除重装.怎知道,我在瘟都死下删除linux的分区,结果我的两个瘟都死分区也没了,哭了我去恢复数据,但是然并卵. ...

  5. NestedScrollingParent, NestedScrollingChild 详解

    之前听同事提起过 NestedScrollingView,但是一直没有时间去了解,最近一段时间比较空,才开始去了解.先点开,看 NestedScrollingView 源码: public class ...

  6. Hyper-V 虚拟网络设置

    目标:搭建一个主机上的网络用来链接主机和虚拟机,并且虚拟机可以通过主机上网. 步骤一:创建一个Internal Network. 步骤二: 创建虚拟机并设置Virtual Switch. 步骤三:将上 ...

  7. Flask 框架 简介

    一.Flask介绍 Flask是一个基于Werkzeug,Jinja 2 轻量级的web开发框架, 使用Python开发, 上手简单. 二.安装Flask 三.第一个Flask程序 1.编写app.p ...

  8. 日志采集框架Flume以及Flume的安装部署(一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统)

    Flume支持众多的source和sink类型,详细手册可参考官方文档,更多source和sink组件 http://flume.apache.org/FlumeUserGuide.html Flum ...

  9. UGUI 粒子特效与UI层级问题

    游戏中,界面上有些按钮之上需要放置一个特效,或者有些区域显示比如image上显示一个特效,这时候如果再打开一个UI,我们需要让新的UI显示在特效上层,而不是被特效遮挡,这是就需要设置特效的渲染顺序. ...

  10. UWP 手绘视频创作工具技术分享系列 - 全新的 UWP 来画视频

    从2017年11月开始,我们开始规划和开发全新的来画Pro,在12月23日的短视频峰会上推出了预览版供参会者体验,得到了很高的评价和关注度.吸取反馈建议后,终于在2018年1月11日正式推出了全新版本 ...