LoginSignup
20
24

More than 5 years have passed since last update.

Swift4のCodableでフラットなJSONからネストしたオブジェクトにデコードする

Last updated at Posted at 2017-06-08

init(from:)で独自のデコード処理を記述することが出来ます。以下のコードでフラットなJSONからネストしたオブジェクトにデコードすることが出来ます。

struct User: Codable {
    struct Address: Codable {
        let street: String
        let city: String
        let state: String
    }
    let name: String
    let address: Address

    private enum CodingKeys: String, CodingKey {
        case name
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)
        address = try Address(from: decoder)
    }
}

let data = """
{
"name": "rizumita",
"street": "1-2-3 Foo",
"city": "Bar",
"state": "Baz"
}
""".data(using: .utf8)!

let decoder: JSONDecoder = JSONDecoder()
do {
    let user: User = try decoder.decode(User.self, from: data)
    print(user)
} catch {
    print(error)
}
20
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
24