I'm pretty green when it comes to working with data flow but I'm trying to force myself through the exercise of building an application to learn it. I'm struggling with how to write my model to correctly reference nested keys in a JSON file.
The JSON looks like this:
[{
"id": 1001,
"first_name": "Jimmy",
"last_name": "Simms",
"cities": [{
"name": "New York City",
"towns": [{
"name": "Brooklyn"
},
{
"name": "Manhatten"
}
]
},
{
"name": "Tokyo",
"towns": [{
"name": "Churo"
},
{
"name": "Riponggi"
}
]
}
]
}]
What I'm unsure how to do is write a model that lets me target nested elements inside the arrays. Broadly speaking, I'm wondering how you might right a SwiftUI model for this JSON set.
This was kind of my stab in the dark at it:
import SwiftUI
struct MyDataModel: Identifiable {
var id: Int
var first_name: String
var last_name: String
var cities: Array<Any>
var cities.name: String
var cities.towns: Array<Any>
var cities.towns.name: String
}
But my syntax for accessing the nested arrays "Cities" and then the second level "Towns" is just made up and I can find a reference for how to access nested key's like this? Any help would be much appreciated.
