iOS 解碼 & 編碼 JSON 的文章整理彼得潘的 iOS App Neverland·FollowPublished in彼得潘的 Swift iOS App 開發問題解答集·10 min read·Dec 25, 2020--Share利用 JSONDecoder 和 Codable 解析 JSON 和生成自訂型別資料利用 JSONDecoder 和 Codable 解析 JSON 和生成自訂型別資料抓取網路上的 JSON 資料並不是太困難的事,但是如果想要解析它,甚至是把它變成方便 App 使用的自訂型別,卻需要寫許多程式碼才能實現。因此從前一些解析 JSON 和將 JSON 變成自訂型別的第三方套件大受歡迎,不過在 Swift…medium.com利用 Smart JSON Editor 查看 JSON 的資料型別利用 Smart JSON Editor 查看 JSON 的資料型別透過 Postman App,瀏覽器的外掛或是一些線上網站的幫忙,我們可以將 API 抓取的 JSON 資料變得美美的,比方透過網站 JSON Editor Online 將 JSON 資料變成可展開的美美樹狀圖。medium.com解析 JSON 時,Decodable 型別加 ? 的情況解析 JSON 時,Decodable 型別加 ? 的情況當我們利用 JSONDecoder 將 JSON 資料變成自訂的 Decodable 型別時,property 在以下兩種情況必須加上問號,宣告為 optional,因為有可能找不到對應的 value。medium.com顯示美美 JSON 的 JSON Editor Online利用 JSON Editor Online 排版美美的 JSON 資料開發 iOS App 時,我們時常串接 API 抓取 JSON 格式的資料。不過 JSON 卻讓我們又愛又恨,它豐富了我們的 App 內容,但要看懂 JSON 卻不是件易事。medium.com找出 JSONDecoder 的 decode 錯誤找出 JSONDecoder 的 decode 錯誤利用 JSONDecoder,我們可以很方便地將 API 回傳的 JSON 資料轉成遵從 Decodable 的自訂型別,例如以下結合 Dog API,抓取可愛小狗圖片的例子。medium.com利用 JSONSerialization 印出美美縮排的 JSON利用 JSONSerialization 印出美美縮排的 JSON串接後台的 JSON API 時,我們時常需要檢查 JSON 資料是否正確。透過將抓到的 Data 變成 String,我們可以用厲害的肉眼檢查。medium.com利用中斷點將 JSON Data 變成字串利用中斷點將 JSON Data 變成字串開發 iOS App 時,我們時常串接 API 抓取 JSON 資料。不過寫 code 寫多了,難免遇到 bug,有時在 JSONDecoder 解析 JSON 時會遇到問題。此時我們通常有兩種解法:medium.com利用 keyDecodingStrategy 的 convertFromSnakeCase 自動轉換 JSON 欄位名稱利用 keyDecodingStrategy 的 convertFromSnakeCase 自動轉換 JSON 欄位名稱 — Swift 4.1利用 JSONDecoder,現在我們不再需要第三方套件,很方便就能將 JSON 資料轉換成自訂型別。medium.com利用 CodingKey 客製 JSON 對應的 property利用 enum CodingKeys 客製 JSON 對應的 property利用 JSONDecoder 我們可以方便地串接 API,將網路上的 JSON 資料轉換成方便 App 使用的自訂型別。medium.com自訂 custom 的 keyDecodingStrategy,將 JSON 的大寫 key 變小寫自訂 custom 的 keyDecodingStrategy,將 JSON 的大寫 key 變小寫使用 JSONDecoder 解碼 JSON 資料時,有時我們會遇到 JSON 裡的 key 跟 Swift 變數命名習慣不同,比方彼得潘最近串接公車 API,發現它的 key 都是大寫開頭,跟 Swift 習慣小寫開頭的變數名稱不合。medium.comJSONDecoder 解析時間的 DateDecodingStrategyJSONDecoder 解析時間的 DateDecodingStrategy當我們利用 JSONDecoder 串接 API 回傳的 JSON 資料時,通常我們會將時間欄位的資料對應到 Date 型別,方便之後App 使用一些時間運算的 function。不過 API 回傳的時間有許多格式,因此我們須告訴…medium.com讀取專案裡的檔案測試 JSON 解碼讀取專案裡的檔案測試 JSON 解碼為了測試 JSON 資料對應的 Decodable(或 Codable) 型別是否有問題,除了實際串接 API 抓資料,我們也可以先將 JSON 檔加入專案裡,方便沒有網路時進行測試。medium.com利用 Swift Error Breakpoint 檢查 error handling 丟出的錯誤利用 Swift Error Breakpoint 檢查 error handling 丟出的錯誤開發 iOS App 時,我們有時會遇到一些丟出錯誤的程式。當我們使用 do catch 或 try! 時,可以馬上看到錯誤的相關資訊,比方以下程式 JSONDecoder 因解析 JSON 失敗丟出錯誤。medium.com利用 Postman,Development Assets & Code snippet 驗證 Codable 型別將 JSON 轉換成 Codable 自訂型別的 10 秒驗證方法串接第三方 API,將 JSON 以 JSONDecoder 轉換成自訂的 Codable 型別是許多 App 上常見的功能。不過初學者剛開始常會在定義 JSON 對應的 Codable 型別時出問題,因此彼得潘接下來將以從…medium.com定義 Decodable 的 init(from:) 解析 JSON定義 Decodable 的 init(from:) 解析 JSON透過 JSONDecoder,我們可以方便地將 JSON 資料變成遵從 protocol Decodable 的自訂型別,而且大部分的時候我們只要宣告型別裡的 property,不用自己撰寫解析 JSON 的 init(from…medium.com在 Codable 型別裡宣告跟 JSON 資料無關的 property在 Codable 型別裡宣告跟 JSON 資料無關的 property有時我們會想在遵從 Codable 的型別裡宣告跟 JSON 資料無關的 property。接下來讓我們以 Meme API 的 JSON 為例,介紹 4 種不同的方法。medium.com貼上 JSON,自動產生 Swift Codable 型別的 quicktypeInstantly parse JSON in any language | quicktypeWhether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for…app.quicktype.io使用 google 表單製作 iOS App 開發練習的假資料 array使用 google 表單製作 iOS App 開發練習的假資料 array學習開發 iOS App 時,很多元件都要透過 array 設定顯示的內容,比方使用 SwiftUI 的 List 或 UIKit 的 UITableView 呈現以下的 Christmas 歌單頁面。medium.com方便 App 測試假(mock)的 JSON 資料的平台方便 App 測試假(mock)的 JSON 資料的平台開發 iOS App 時,我們時常需要連線到後台抓取 JSON 資料。然而有時我們會想有個回傳假的 JSON 資料的後台,比方以下情況:medium.com使用 Mac 當後台,存放測試的圖片和 JSON使用 Mac 當後台,存放測試的圖片和 JSON開發 iOS App 時,我們時常需要透過網路連結後台抓資料。不過若是還在測試階段,倒是可以使用本機 Mac 當後台,然後將測試的圖片和 JSON 檔放在 /Library/WebServer/Documents,方法如下:medium.com利用 JSONDecoder 將資料變成遵從 Decodable 的 enum利用 JSONDecoder 將資料變成遵從 Decodable 的 enum當我們從 API 抓取 JSON 資料時,有些欄位的內容雖然是字串或數字,但卻很適合變成 enum,提高程式的可讀性跟安全性。medium.com利用 CodingKey & init(from:) 將不同的 JSON key 對應到同一個 property利用 CodingKey & init(from:) 將不同的 JSON key 對應到同一個 property串接 JSON API 時,有時我們會遇到 JSON 的某個 key 名字有多種可能,而我們希望能將它們對應到同一個 property。medium.com透過 init(from:) 解析 JSON 裡包裝成字串的 array 或 dictionary透過 init(from:) 解析 JSON 裡包裝成字串的 array 或 dictionary透過 JSONDecoder,我們可以將 JSON 資料變成自訂的型別。不過當 JSON 裡 array 或 dictionary 被包裝成字串時,解析上將麻煩許多。例如以下英國 COVID-19 API 的 JSON。medium.com透過 encode(to:) 將 JSON 裡的 array 或物件變成字串上傳透過 encode(to:) 將 JSON 裡的 array 或物件變成字串上傳透過 JSONEncoder 的幫忙,我們可以將自訂型別的資料轉換成 Data 型別,上傳 JSON 格式的資料到後台。例如以下例子:medium.com開啟很大的 JSON 檔的軟體 — Dadroit JSON Viewer開啟很大的 JSON 檔的軟體 — Dadroit JSON ViewerXcode 開啟 JSON 檔不是問題,但是當它遇到很大的 JSON 檔時,它將需要花很久很久的時間。medium.com其它參考資源Using JSON with Custom Types | Apple Developer DocumentationJSON data you send or receive from other apps, services, and files can come in many different shapes and structures…developer.apple.comCodable cheat sheetCodable was one of the cornerstone features of Swift 4.0, bringing with it incredibly smooth conversion between Swift…www.hackingwithswift.comBen ScheirmanThis guide is now permalinked at http://swiftjson.guide Swift 4 and Foundation has finally answered the question of how…benscheirman.comEncoding, Decoding and Serialization in Swift 4This is an abridged chapter from our best-selling book Swift Apprentice, which has been completely updated for Swift 4…www.raywenderlich.com