Hi everyone,
I need help pulling individual data out of a JSON file I made for an app I am building. The JSON might not be set up ideally, so please feel free to comment on that, too. (I eventually want to filter the meridian element chosen and then pass the point name to my UI, as well as eventually showing a photo + description in a dedicated view)
I am able to successfully decode the JSON file, so when I pull up the count I get the right number (12), but I cant seem to figure out how to get to the individual data points. I am linking my GitHub repo with all of the files, and pasting the relevant parts of my code here.
Please help, I have been stuck on this for weeks, I redid the Moonshot app 3 times, and have read anything I could on working with JSONs, but I can't seem to get unstuck, and the more I read, the more I dig myself in this rut.
Meridian and Acupoint Structs + ContentView
struct Meridian: Codable, Identifiable {
let id: String
let yinYang: String
let points: [Acupoint]
}
struct Acupoint: Codable, Identifiable {
let id: String
let name: String
let level: Int?
let element: String
let description: String
}
struct ContentView: View {
let meridians: [String: Meridian] = Bundle.main.decode("meridiansNacupoints.json")
var body: some View {
VStack(alignment: .leading){
Text("Total meridians: \(meridians.count)") // currently correctly prints "12"
Text("First meridian: ") // should print -> "Liver"
Text("Acupoints on meridian: ") // liver.points.count, should print -> "6"
Text("Element of meridian: ") // should print -> "wood"
HStack {
VStack(alignment: .leading) {
Text("Level 2 point: ")
}
VStack(alignment: .trailing) {
Text("-") // name, should print -> "Liv-2"
Text("-") // element, should print -> "fire"
Text("-") // description, should print -> "Description"
}
}
}
.padding()
}
}
JSON (shortened for convenience's sake)
{
"liver": {
"id": "liver",
"element": "wood",
"yinYang": "yin",
"points": [
{
"id": "LIV-1",
"name": "LIV-1",
"level": 1,
"element": "wood",
"description": "Description"
},
{
"id": "LIV-2",
"name": "LIV-2",
"level": 2,
"element": "fire",
"description": "Description"
},
... more points here with th same format...
]
},
... more meridians here with the same format ...
}