本例子主要是使用由中央气象局网站(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. Swift学习第一天--面向过程

    //: Playground - noun: a place where people can play import UIKit //---------------------- Hello wor ...

  2. JAVA NIO学习二:通道(Channel)与缓冲区(Buffer)

    今天是2018年的第三天,真是时光飞逝,2017年的学习计划还没有学习完成,因此继续开始研究学习,那么上一节我们了解了NIO,那么这一节我们进一步来学习NIO相关的知识.那就是通道和缓冲区.Java ...

  3. requireJS教程

    目录[-] 使用 RequireJS 优化 Web 应用前端 AMD 简介 传统 JavaScript 代码的问题 AMD 的引入 清单 1. AMD 规范 RequireJS 简介 实战 Requi ...

  4. php解决json_encode输出GB2312中文问题 (数组)

    在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存储格式. ...

  5. 测试库的接收到的数据是否完整(jrtplib为列)

    最近使用jrtplib来接收RTP包,然后解码播放 发现解码出来的是绿屏,马赛克 于是开始排查 首先直接用wireshark抓进来的包,转为可以被vlc播放的文件 操作如下 http://blog.c ...

  6. mysql 基础指令/命令

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root - ...

  7. 微信小程序入门指南

    本文同步发布在 https://www.cssge.com 因为下个项目需要用微信小程序来开发,所以就找了小程序开发文档来研究.下面记录一下微信小程序的主要开发流程和语法. 账号注册 开发小程序的第一 ...

  8. shell 变量的间接引用

    参考: 高级 bash 脚本编程指南 eval var1=\$$var2 变量间接引用 贴个脚本: server1=1.1.1.1 server2=1.1.1.2 server3=1.1.1.3 fo ...

  9. dotnet core 自定义配置文件

    首先添加一个.json 文件,比如 setting.json 文件内容如下,记得把文件设置为“复制到输出目录” { "ConfigSetting": { "XXXName ...

  10. jQuery 属性(十二)

    属性 描述 context 在版本 1.10 中被弃用.包含传递给 jQuery() 的原始上下文. jquery 包含 jQuery 版本号. jQuery.fx.interval 改变以毫秒计的动 ...