iT邦幫忙

2025 iThome 鐵人賽

DAY 27
0
生成式 AI

Swift一下就會了系列 第 27

Day 27 天氣API 1

  • 分享至 

  • xImage
  •  

這次我們要來練習下一個專案天氣API

在這個專案中,我們會學習如何從網路上抓取外部資料
並將這些資料顯示在我們的 App 裡。

先來看看這次的成品畫面:
https://ithelp.ithome.com.tw/upload/images/20251011/20178746VUtYzNaS15.png
https://ithelp.ithome.com.tw/upload/images/20251011/201787460b6qAVkp58.png

要使用開放的天氣資料,我們首先要去申請一個 中央氣象局網站帳號
https://www.cwa.gov.tw/V8/C/S/eservice/member.html
https://ithelp.ithome.com.tw/upload/images/20251011/20178746uziJcur1oH.png
申請完之後我們到氣象資料開放平台登入。
https://opendata.cwa.gov.tw/index
並找到自己的授權碼。
https://ithelp.ithome.com.tw/upload/images/20251011/20178746GZDqHgPVik.png
這邊要記好自己的授權碼喔。

接著我們來到中央氣象局給的資料API。
https://opendata.cwa.gov.tw/dist/opendata-swagger.html#/預報/get_v1_rest_datastore_F_C0032_001
https://ithelp.ithome.com.tw/upload/images/20251011/20178746VyjH9uJWAW.png
這邊我們拿第一個當作練習。
https://ithelp.ithome.com.tw/upload/images/20251011/20178746ztt6l4MKz9.png
點選右邊的Try it out按鈕。

接下來會出現一個輸入欄位,在這裡把剛剛取得的授權碼貼上去。
https://ithelp.ithome.com.tw/upload/images/20251011/20178746m4NPkHA2ll.png
中間的欄位都可以保持預設值,最後往下滑,按下最底下的藍色按鈕 Execute
https://ithelp.ithome.com.tw/upload/images/20251011/20178746qRg12cQl48.png
執行後,頁面就會回傳一大串 JSON 格式 的資料。
https://ithelp.ithome.com.tw/upload/images/20251011/20178746a6YJO7maMU.png
不過這些資料直接看起來有點混亂,因為層級太多、又沒有排版。

我們就要使用到下一個小工具。
http://json.parser.online.fr
https://ithelp.ithome.com.tw/upload/images/20251011/20178746xozB5b19dV.png
我們只要把剛剛從 API 網頁中複製出來的 JSON 資料,貼到左邊的欄位中,就可以在右邊看到整齊、分層的資料了。
https://ithelp.ithome.com.tw/upload/images/20251011/20178746RmIkkowY3k.png
這樣就能比較方便的取用了喔。

結語

今天我們先完成了**天氣 API **的第一步,明天我們還要繼續來使用上面的資料喔。


上一篇
Day 26 時鐘 5
系列文
Swift一下就會了27
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言