CodableでJSONをデコードする
JSONをSwiftのモデル構造体にマッピングします。
「CodableでJSONをデコードする」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why JSON Needs Decoding
APIs speak JSON, a text format Swift cannot use directly. You decode that text into real Swift types you can work with. 📦
Meet Codable
The Codable protocol lets a type convert itself to and from JSON automatically, so you rarely write parsing code by hand.
A Codable Struct
Just add Codable to your struct. Swift generates the encoding and decoding for every stored property for you.
struct User: Codable {
let id: Int
let name: String
}Matching JSON Keys
Swift maps each property name straight to the matching JSON key. So "name" in JSON fills the name property exactly.
{ "id": 1, "name": "Ada" }Decoding with JSONDecoder
A JSONDecoder turns raw Data into your type. You tell it which type to expect, and it does the rest.
let user = try JSONDecoder()
.decode(User.self, from: data)Working With Data
Network responses arrive as Data, a bag of bytes. The decoder reads those bytes and produces your typed value.
Decoding Arrays
To decode a JSON list, ask for an array of your type. One call gives you a ready-to-use Swift array. 🎉
let users = try JSONDecoder()
.decode([User].self, from: data)Renaming with CodingKeys
When JSON keys differ from your names, add CodingKeys to map a snake_case key onto a clean Swift property.
enum CodingKeys: String, CodingKey {
case name = "full_name"
}Optional Properties
Mark a property optional when the key may be missing. The decoder simply sets it to nil instead of throwing.
let avatar: String?Decoding Can Throw
If the JSON shape does not match your type, decoding throws. Always wrap the call in try so you can handle bad data.
Nested Models
Nested JSON objects become nested structs. Make each level Codable and Swift decodes the whole tree at once.
struct Post: Codable {
let author: User
}Quick Check
Quick check on turning JSON into Swift types.
Recap: Codable
You learned that Codable plus JSONDecoder maps JSON into typed Swift values, with CodingKeys for renames and optionals for missing keys. 🙌
よくある質問
「CodableでJSONをデコードする」レッスンは無料ですか?
はい。「CodableでJSONをデコードする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「CodableでJSONをデコードする」で何を学びますか?
JSONをSwiftのモデル構造体にマッピングします。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「CodableでJSONをデコードする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- CodableでJSONをデコードする
- URLSessionでAPIを呼び出す
- .taskモディファイア
- 読み込み中とエラーの状態