Closed icanswiftabit closed 8 years ago
Hi I love Decodable but recently I encounter this situation with nested JSON.
JSON
{ "cars": [ { "name": "bmw", "passagers": [ { "name": "john", "age": "23" }, { "name": "lucy", "age": "23" } ] }, { "name": "ford", "passagers": [ { "name": "błażej", "age": "27" }, { "name": "marcy", "age": "27" } ] } ] }
Structs
struct Car:Decodable{ var name:String var passagers:[Passager] static func decode(json:AnyObject) throws -> Passager { return try DirectionJson( name: json => "name", passagers: json => [Passager].decode //? error, so how? ) } } struct Passager:Decodable{ var name:String var age:Int static func decode(json:AnyObject) throws -> Passager { return try DirectionJson( name: json => "name", age: json => "age" ) } }
I was hoping that whole json would be decoded by single [Car].decode(json). But I'm not sure how I should handle [Passager] in Car.
[Car].decode(json)
[Passager]
Car
It should be enough to write passagers: json => "passagers", and the type inference should figure it out.
passagers: json => "passagers"
That thank that works great. Again thank for you work.
Hi I love Decodable but recently I encounter this situation with nested JSON.
JSON
Structs
I was hoping that whole json would be decoded by single
[Car].decode(json)
. But I'm not sure how I should handle[Passager]
inCar
.