[C #] Using the console to get the weather forecast

Source: Internet
Author: User

This example uses the JSON API provided by the Central Meteorological Administration Website (http://www.nmc.gov.cn) to implement the following ideas:

1, access to the province (including municipalities, autonomous regions, etc., hereinafter referred to as the province) of the website (http://www.nmc.gov.cn/f/rest/province), return the corresponding province name (name), code, etc., as shown in:

2, according to the code returned above, the code is stitched at the back of the URL (http://www.nmc.gov.cn/f/rest/province), such as the Returned code is AGD (Guangdong province), the post-splicing URL is http// Www.nmc.gov.cn/f/rest/province/AGD to obtain the corresponding city name, code, as shown in:

3, according to the code returned above, the code is spliced at the back of the URL (http://www.nmc.gov.cn/f/rest/real/), such as the return code is 59287 (Guangzhou), the post-splicing URL is http://www.nmc.gov.cn/f/ rest/real/59287 to obtain the corresponding weather information as shown in:

4, this example uses the technology for the HttpWebRequest class, the HttpWebResponse class and the Newtonsoft.Json.JsonConvert class the use, oneself has not understood, please carry on the Baidu by oneself;

5, the source code is as follows:

usingNewtonsoft.json;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;namespaceweather{classProgram {Static voidMain (string[] args) {HttpWebRequest Request= Webrequest.createhttp (@"http://www.nmc.gov.cn/f/rest/province"); Try{HttpWebResponse response= Request. GetResponse () asHttpWebResponse; Stream Stream=Response.                GetResponseStream (); StreamReader Reader=NewStreamReader (stream); stringContent =Reader.                ReadToEnd (); List<Province> Provinceresult = jsonconvert.deserializeobject<list<province>>(content); Dictionary<string,string> provicenamedict =Newdictionary<string,string>(); Console.WriteLine ("Provinces and municipalities:"); Provinceresult.foreach (x={provicenamedict.add (x.name, X.code);                Console.WriteLine (X.name);                }); stringProvice;  while(true) {Console.Write ("Please enter a province or municipality to query:"); 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 () asHttpWebResponse; Stream=Response.                GetResponseStream (); Reader=NewStreamReader (stream); Content=Reader.                ReadToEnd (); List<City> Cityresult = jsonconvert.deserializeobject<list<city>>(content); Dictionary<string,string> citynamedict =Newdictionary<string,string>(); Console.WriteLine ("City:"); Cityresult.foreach (x={citynamedict.add (x.city, X.code);                Console.WriteLine (x.city);                }); stringCity ;  while(true) {Console.Write ("Please enter the city you want to query:"); 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 () asHttpWebResponse; Stream=Response.                GetResponseStream (); Reader=NewStreamReader (stream); Content=Reader.                ReadToEnd (); Detail Detailresult= jsonconvert.deserializeobject<detail>(content); Console.WriteLine (New string('-', -)); Console.WriteLine ("detailed information is as follows:"); Console.WriteLine ($"{detailresult.station.province},{detailresult.station.city} release time: {detailresult.publish_time}"); Console.WriteLine ($"temperature: {Detailresult.weather.temperature}℃ temperature difference: {Detailresult.weather.temperaturediff}℃ pressure: { Detailresult.weather.airpressure}hpa Humidity: {detailresult.weather.humidity}% rain: {detailresult.weather.rain}mm"); Console.WriteLine ($"weather conditions: {DetailResult.weather.info}"); Console.WriteLine ($"wind: {detailResult.wind.direct} {DetailResult.wind.power} wind Speed: {detailresult.wind.speed}m/s"); Console.WriteLine (New string('-', -)); }            Catch(WebException ex) {Console.WriteLine (ex).            Message); } Console.WriteLine ("Press any key to exit ...");        Console.readkey (); }    }    classProvince { Public stringCode {Set;Get; }  Public stringName {Set;Get; }  Public stringURL {Set;Get; } }    classCity { Public stringURL {Set;Get; }  Public stringCode {Set;Get; }  Public stringCity {Set;Get; }  Public stringProvince {Set;Get; } }    classDetail { PublicCity Station {Set;Get; }  Public stringPublish_time {Set;Get; }  PublicWeather Weather {Set;Get; }  PublicWind Wind {Set;Get; }  PublicWarn Warn {Set;Get; } }    classWeather { Public floattemperature {Set;Get; }  Public floatTemperaturediff {Set;Get; }  Public floatairpressure {Set;Get; }  Public floatHumidity {Set;Get; }  Public floatRain {Set;Get; }  Public floatRcomfort {Set;Get; }  Public floatIcomfort {Set;Get; }  Public stringinfo {Set;Get; }  Public stringIMG {Set;Get; }  Public floatFeelst {Set;Get; } }    classWind { Public stringDirect {Set;Get; }  Public stringPower {Set;Get; }  Public floatSpeed {Set;Get; } }    classWarn { Public stringAlert {Set;Get; }  Public stringPic {Set;Get; }  Public stringProvince {Set;Get; }  Public stringCity {Set;Get; }  Public stringURL {Set;Get; }  Public stringissuecontent {Set;Get; }  Public stringFmeans {Set;Get; } }}

6, the operation effect is as follows:

7. The source code and the executable application are as follows:

Source code: Https://pan.baidu.com/s/1pM98VnP
Executable Application: HTTPS://PAN.BAIDU.COM/S/1I6MK8XN

[C #] Using the console to get the weather forecast

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.