I am able to show the name, cost per launch, etc of the JSON. However I cannot access the value for payload_weights and flic_images. I think because they are inside an array of objects.
Here is part of the JSON file
[
{
"rocketid": 1,
"id": "falcon1",
"name": "Falcon 1",
"type": "rocket",
"active": false,
"stages": 2,
"boosters": 0,
"cost_per_launch": 6700000,
"success_rate_pct": 40,
"first_flight": "2006-03-24",
"country": "Republic of the Marshall Islands",
"company": "SpaceX",
"height": {
"meters": 22.25,
"feet": 73
},
"diameter": {
"meters": 1.68,
"feet": 5.5
},
"mass": {
"kg": 30146,
"lb": 66460
},
"payload_weights": [
{
"id": "leo",
"name": "Low Earth Orbit",
"kg": 450,
"lb": 992
}
]
This is my model:
struct Rocket: Codable, Identifiable {
var id: String
var name: String
var type: String
var active: Bool
var stages: Int
var boosters: Int
var cost_per_launch: Int
struct PayloadWeight: Codable {
var id: String
var name: String
var kg: Int
var lb: Int
}
var payload_weights: [PayloadWeight]
}
This is the view I want to show my data.
struct ContentView: View {
let rockets = Bundle.main.decode([Rocket].self, from: "Rocket.json")
var body: some View {
List(rockets) { rocket in
HStack {
Text(rocket.name)
}
}
}
}