C# 天氣預報

来源:http://www.cnblogs.com/hsiang/archive/2017/04/04/6665194.html
-Advertisement-
Play Games

本文通過一個完整的實例,講解如何通過訪問中國天氣網提供的API介面,實現天氣預報功能。 實現天氣預報功能的相關知識點: 天氣預報介面【提供風向,風速等功能】:http://www.weather.com.cn/data/sk/101120201.html 天氣預報介面【提供天氣基礎功能】:http: ...


本文通過一個完整的實例,講解如何通過訪問中國天氣網提供的API介面,實現天氣預報功能。

實現天氣預報功能的相關知識點:

  • 天氣預報介面【提供風向,風速等功能】:http://www.weather.com.cn/data/sk/101120201.html
  • 天氣預報介面【提供天氣基礎功能】:http://www.weather.com.cn/data/cityinfo/101120201.html
  • 新浪網的介面【其中city後的是城市的名稱轉碼】http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0
  • HttpWebRequest/HttpWebResponse 訪問網路的相關資源
  • XmlSerializer 中序列化與反序列化功能
  • Newtonsoft.Json中Json字元串的反序列化功能
  • 泛型,委托

具體如圖所示:

天氣代碼,對接天氣API介面返回的實體類,兩個介面對應兩個實體類:

 

  1 using Newtonsoft.Json;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 using System.Xml.Serialization;
  8 
  9 namespace DemoWeather
 10 {
 11     /// <summary>
 12     /// 城市基礎天氣信息類
 13     /// </summary>
 14     [Serializable]
 15     public class CWeather
 16     {
 17         [JsonProperty("weatherinfo")]
 18         public CWeatherInfo WeatherInfo { get; set; }
 19     }
 20 
 21     [Serializable]
 22     public class CWeatherInfo
 23     {
 24         [JsonProperty("city")]
 25         public string City { get; set; }
 26 
 27         [JsonProperty("cityid")]
 28         public string CityId { get; set; }
 29 
 30         /// <summary>
 31         /// 氣溫1
 32         /// </summary>
 33         [JsonProperty("temp1")]
 34         public string Temp1 { get; set; }
 35 
 36         /// <summary>
 37         /// 氣溫2
 38         /// </summary>
 39         [JsonProperty("temp2")]
 40         public string Temp2 { get; set; }
 41 
 42         /// <summary>
 43         /// 天氣情況
 44         /// </summary>
 45         [JsonProperty("weather")]
 46         public string Weather { get; set; }
 47 
 48         [JsonProperty("img1")]
 49         public string Img1 { get; set; }
 50 
 51         [JsonProperty("img2")]
 52         public string Img2 { get; set; }
 53 
 54         /// <summary>
 55         /// 更新時間
 56         /// </summary>
 57         [JsonProperty("ptime")]
 58         public string PTime { get; set; }
 59 
 60     }
 61 
 62     /// <summary>
 63     /// 城市基礎天氣信息類(另外一個)
 64     /// </summary>
 65     [Serializable]
 66     public class DWeather
 67     {
 68         [JsonProperty("weatherinfo")]
 69         public DWeatherInfo WeatherInfo { get; set; }
 70     }
 71 
 72     [Serializable]
 73     public class DWeatherInfo {
 74         [JsonProperty("city")]
 75         public string City { get; set; }
 76 
 77         [JsonProperty("cityid")]
 78         public string CityId { get; set; }
 79 
 80         /// <summary>
 81         /// 氣溫1
 82         /// </summary>
 83         [JsonProperty("temp")]
 84         public string Temp { get; set; }
 85 
 86         /// <summary>
 87         /// 風向
 88         /// </summary>
 89         [JsonProperty("WD")]
 90         public string WD { get; set; }
 91 
 92         /// <summary>
 93         /// 風速
 94         /// </summary>
 95         [JsonProperty("WS")]
 96         public string WS { get; set; }
 97 
 98         /// <summary>
 99         /// 相對濕度
100         /// </summary>
101         [JsonProperty("SD")]
102         public string SD { get; set; }
103 
104         /// <summary>
105         /// 風力
106         /// </summary>
107         [JsonProperty("WSE")]
108         public string WSE { get; set; }
109 
110         /// <summary>
111         /// 更新時間
112         /// </summary>
113         [JsonProperty("time")]
114         public string Time { get; set; }
115 
116         /// <summary>
117         /// 是否有雷達圖(1表示有雷達圖)
118         /// </summary>
119         [JsonProperty("isRadar")]
120         public string IsRadar { get; set; }
121 
122         /// <summary>
123         /// 雷達圖地址(AZ9532為青島雷達)
124         /// </summary>
125         [JsonProperty("Radar")]
126         public string Radar { get; set; }
127 
128         /// <summary>
129         /// 
130         /// </summary>
131         [JsonProperty("njd")]
132         public string Njd { get; set; }
133 
134         [JsonProperty("qy")]
135         public string Qy { get; set; }
136 
137         [JsonProperty("rain")]
138         public string Rain { get; set; }
139 
140     }
141 
142     /// <summary>
143     /// 新浪天氣預報API實體類
144     /// </summary>
145     [Serializable]
146     [XmlRoot("Profiles")]
147     public class SWeather {
148 
149         [JsonProperty("Weather")]
150         [XmlElementAttribute("Weather")]
151         public SWeatherInfo WeatherInfo { get; set; }
152     }
153 
154     public class SWeatherInfo {
155 
156         /// <summary>
157         /// 對應的查詢城市
158         /// </summary>
159         [JsonProperty("city")]
160         [XmlElementAttribute("city")]
161         public string City { get; set; }
162 
163         /// <summary>
164         /// 白天天氣情況
165         /// </summary>
166         [JsonProperty("status1")]
167         [XmlElementAttribute("status1")]
168         public string Status1 { get; set; }
169 
170         /// <summary>
171         /// 夜間天氣情況
172         /// </summary>
173         [JsonProperty("status2")]
174         [XmlElementAttribute("status2")]
175         public string Status2 { get; set; }
176 
177         /// <summary>
178         /// 白天天氣情況拼音
179         /// </summary>
180         [JsonProperty("figure1")]
181         [XmlElementAttribute("figure1")]
182         public string Figure1 { get; set; }
183 
184         /// <summary>
185         /// 夜間天氣情況拼音
186         /// </summary>
187         [JsonProperty("figure2")]
188         [XmlElementAttribute("figure2")]
189         public string Figure2 { get; set; }
190 
191         /// <summary>
192         /// 白天風向
193         /// </summary>
194         [JsonProperty("direction1")]
195         [XmlElementAttribute("direction1")]
196         public string Direction1 { get; set; }
197 
198         /// <summary>
199         /// 夜晚風向
200         /// </summary>
201         [JsonProperty("direction2")]
202         [XmlElementAttribute("direction2")]
203         public string Direction2 { get; set; }
204 
205         /// <summary>
206         /// 白天風力
207         /// </summary>
208         [JsonProperty("power1")]
209         [XmlElementAttribute("power1")]
210         public string Power1 { get; set; }
211 
212         /// <summary>
213         /// 夜間風力
214         /// </summary>
215         [JsonProperty("power2")]
216         [XmlElementAttribute("power2")]
217         public string Power2 { get; set; }
218 
219         /// <summary>
220         /// 白天溫度
221         /// </summary>
222         [JsonProperty("temperature1")]
223         [XmlElementAttribute("temperature1")]
224         public string Temperature1 { get; set; }
225 
226         /// <summary>
227         /// 夜間溫度
228         /// </summary>
229         [JsonProperty("temperature2")]
230         [XmlElementAttribute("temperature2")]
231         public string Temperature2 { get; set; }
232 
233         /// <summary>
234         /// 體感指數
235         /// </summary>
236         [JsonProperty("ssd")]
237         [XmlElementAttribute("ssd")]
238         public string Ssd { get; set; }
239 
240         /// <summary>
241         /// 白天體感溫度
242         /// </summary>
243         [JsonProperty("tgd1")]
244         [XmlElementAttribute("tgd1")]
245         public string Tgd1 { get; set; }
246 
247         /// <summary>
248         /// 夜間體感溫度
249         /// </summary>
250         [JsonProperty("tgd2")]
251         [XmlElementAttribute("tgd2")]
252         public string Tgd2 { get; set; }
253 
254         /// <summary>
255         /// 紫外線強度
256         /// </summary>
257         [JsonProperty("zwx")]
258         [XmlElementAttribute("zwx")]
259         public string Zwx { get; set; }
260 
261         /// <summary>
262         /// 空調指數
263         /// </summary>
264         [JsonProperty("ktk")]
265         [XmlElementAttribute("ktk")]
266         public string Ktk { get; set; }
267 
268         /// <summary>
269         /// 污染指數
270         /// </summary>
271         [JsonProperty("pollution")]
272         [XmlElementAttribute("pollution")]
273         public string Pollution { get; set; }
274 
275         /// <summary>
276         /// 洗車指數
277         /// </summary>
278         [JsonProperty("xcz")]
279         [XmlElementAttribute("xcz")]
280         public string Xcz { get; set; }
281 
282         /// <summary>
283         /// 綜合指數這個我不確定
284         /// </summary>
285         [JsonProperty("zho")]
286         [XmlElementAttribute("zho")]
287         public string Zho { get; set; }
288 
289         /// <summary>
290         /// 沒猜出來是什麼指數,沒有數值
291         /// </summary>
292         [JsonProperty("diy")]
293         [XmlElementAttribute("diy")]
294         public string Diy { get; set; }
295 
296         /// <summary>
297         /// 同上
298         /// </summary>
299         [JsonProperty("fas")]
300         [XmlElementAttribute("fas")]
301         public string Fas { get; set; }
302 
303         /// <summary>
304         /// 穿衣指數
305         /// </summary>
306         [JsonProperty("chy")]
307         [XmlElementAttribute("chy")]
308         public string Chy { get; set; }
309 
310         /// <summary>
311         /// zho的說明,然而zho是什麼指數我也不確定
312         /// </summary>
313         [JsonProperty("zho_shuoming")]
314         [XmlElementAttribute("zho_shuoming")]
315         public string Zho_shuoming { get; set; }
316 
317         /// <summary>
318         /// 同上
319         /// </summary>
320         [JsonProperty("diy_shuoming")]
321         [XmlElementAttribute("diy_shuoming")]
322         public string Diy_shuoming { get; set; }
323 
324         /// <summary>
325         /// 同上
326         /// </summary>
327         [JsonProperty("fas_shuoming")]
328         [XmlElementAttribute("fas_shuoming")]
329         public string Fas_shuoming { get; set; }
330 
331         /// <summary>
332         /// 穿衣指數說明
333         /// </summary>
334         [JsonProperty("chy_shuoming")]
335         [XmlElementAttribute("chy_shuoming")]
336         public string Chy_shuoming { get; set; }
337 
338         /// <summary>
339         /// 污染程度
340         /// </summary>
341         [JsonProperty("pollution_l")]
342         [XmlElementAttribute("pollution_l")]
343         public string Pollution_l { get; set; }
344 
345         /// <summary>
346         /// 紫外線指數概述
347         /// </summary>
348         [JsonProperty("zwx_l")]
349         [XmlElementAttribute("zwx_l")]
350         public string Zwx_l { get; set; }
351 
352         /// <summary>
353         /// 體感指數概述
354         /// </summary>
355         [JsonProperty("ssd_l")]
356         [XmlElementAttribute("ssd_l")]
357         public string Ssd_l { get; set; }
358 
359         /// <summary>
360         /// 這個不知道
361         /// </summary>
362         [JsonProperty("fas_l")]
363         [XmlElementAttribute("fas_l")]
364         public string Fas_l { get; set; }
365 
366         /// <summary>
367         /// 這個不知道
368         /// </summary>
369         [JsonProperty("zho_l")]
370         [XmlElementAttribute("zho_l")]
371         public string Zho_l { get; set; }
372 
373         /// <summary>
374         /// 穿衣指數概述(可理解為穿衣建議)
375         /// </summary>
376         [JsonProperty("chy_l")]
377         [XmlElementAttribute("chy_l")]
378         public string Chy_l { get; set; }
379 
380         /// <summary>
381         /// 空調指數概述
382         /// </summary>
383         [JsonProperty("ktk_l")]
384         [XmlElementAttribute("ktk_l")]
385         public string Ktk_l { get; set; }
386 
387         /// <summary>
388         /// 洗車指數概述
389         /// </summary>
390         [JsonProperty("xcz_l")]
391         [XmlElementAttribute("xcz_l")]
392         public string Xcz_l { get; set; }
393 
394         /// <summary>
395         /// 這個不知道
396         /// </summary>
397         [JsonProperty("diy_l")]
398         [XmlElementAttribute("diy_l")]
399         public string Diy_l { 
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 作為一隻linux菜的不能更菜的鳥,在linux前臺運行了一個服務後,我必須退出後才能執行其他命令,可是我的服務需要一直運行,於是我同事就給我在該命令後加了">/dev/null 2>&1"據說是這條命令就可以轉向後臺了,我在網上搜索了一下總體意思就是“用來避免shell命令或者程式等運行中有內容輸 ...
  • 時光飛逝,轉眼已經畢業快2年了,覺得自己學的東西多卻不精。對此深深的思考一下,覺得有必要連載unix環境編程文章,以此激勵自己學習。在此立貼為證,2天一篇博客從零開始闡述unix的環境編程。 參考書籍UNIX環境高級編程第三版 ...
  • 1。首先下載安裝vmware 12 pro ,將VT打開(虛擬功能,以前安裝過虛擬機點的同學可忽略)。 2。下載mac ox 10.12正式版鏡像文件(cdr尾碼)。 3。下載Unlocker208(vmware12安裝mac os的補丁)。 開始安裝:① 安裝好vmware 12 pro 後,關閉 ...
  • http://www.pwnable.kr/ 逆向題目,upx脫殼: 找到flag: ...
  • CSRF(Cross-site request forgery)跨站請求偽造,也被稱為“One Click Attack”或者Session Riding,通常縮寫為CSRF或者XSRF,是一種對網站的惡意利用 一般被攻擊步驟: 1.登錄受信任網站A,併在本地生成Cookie。 2.在不登出A的情況 ...
  • 使用C#調用mingw的so文件,拿視頻數據回wpf的界面進行顯示,註冊了回調函數。C++在調用回調函數時遇到了委托被回收的問題,提示:“類型的已垃圾回收委托進行了回調。這可能會導致應用程式崩潰、損壞和數據丟失。向非托管代碼傳遞委托時,托管應用程式必須讓這些委托保持活動狀態,直到確信不會再次調用它們 ...
  • 本文主要介紹如何使用LightningChart擴展拖放功能為所有圖表組件創建圖表,如:系列,標題,軸線等等。支持用滑鼠放置自定義對象到另一個圖表中,如:可以添加或修改JSON/CSV或其他格式的數據。 開發人員也可以使用ChartManager工具來協調多個LightningChart控制項。 支持 ...
  • 1.服務端開發(包含寄宿) 1.1 WCF服務創建方式 創建一個WCF服務,總是會創建一個服務介面和一個服務介面實現。通常根據服務宿主的不同,有兩種創建方式。 (1)創建WCF應用程式 通過創建WCF服務應用程式,可生成一個帶.svc尾碼名的文件。該文件與webform中的aspx文件類似,有後置代 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...