Given a JSON object with nested object structure that looks like this:
{
"users":[
{
"user":{
"name":"Adam",
"age":25
},
"address":{
"city":"Stockholm",
"country":"Sweden"
}
},
{
"user":{
"name":"Lilly",
"age":24
},
"address":{
"city":"Copenhagen",
"country":"Denmark"
}
}
]
}
How can one implement correct Decodable
implementation for an object that looks like this.
struct User {
struct Address {
let city: String
let country: String
}
let name: String
let age: Int
let address: Address
}
Notice that the Swift struct contains a nested struct Address
, while the JSON object has address
in a separate object. Is it possible to create the Decodable
implementation that handles this scenario?
I've tried various approaches, but all of them included creation of intermediary objects that would later map to the User
struct. Is it possible to create an implementation that doesn't involve creation of these intermediary objects?