Using this JSON object as an example:
{
data: [
{
type: "animal"
name: "dog"
consumes: "dog food"
},
{
type: "plant"
name: "cactus"
environment: "desert"
}
]
}
Note the animal
and plant
types have some different properties and some shared properties.
How would JSONDecoder
be used in Swift to convert these into the following structs:
struct Animal: Decodable {
let name: String
let consumes: String
}
struct Plant: Decodable {
let name: String
let environment: String
}