Parse Json file that has multi nest dictionary in Swiftui

30 views Asked by At

I have a Json file like this, I want to access to one (or random one) of the item in the list (it is similar to the question at this Cant parse JSON (nested dictionary) into ForEach with SwiftUI)

{
    "school_1": [
        {
            "department": "Department A",
            "chairman": "Name of Mss",
            "location": "3rd Floor"
        },
        {
            "department": "Department B",
            "chairman": "Name of MssB",
            "location": "3rd Floor"
        }
    ],
    "school_2": [
        {
            "department": "Department AB",
            "chairman": "Name 2 ",
            "location": "Build A "
        },
        {
            "department": "Department ABC",
            "chairman": "Name 5 ",
            "location": "Build B "
        }
    ],
    "school_3": [
        {
            "department": "Department AB",
            "chairman": "Name 2 ",
            "location": "Build A "
        },
        {
            "department": "Department ABC",
            "chairman": "Name 5 ",
            "location": "Build B "
        }
    ],
    "school_A": [
        {
            "department": "Department AB",
            "chairman": "Name 2 ",
            "location": "Build A "
        },
        {
            "department": "Department ABC",
            "chairman": "Name 5 ",
            "location": "Build B "
        }
    ]
"_comment": "similar to 100th school for example"

}

I have tried to buil the models like this:

struct SchoolName: Identifiable {
    let id: String
    let nameDepartment: Department
}

struct Department: Codable {
    let department: String
    let chairman: String
    let location: String
    
    private enum CodingKeys: String, CodingKey {
        case department = "department"
        case chairman = "chairman"
        case location = "location"
    }
}

I create this class to access to any school in the Json but I am not able to get it


class ReadJson: ObservableObject {

    @Published var listSchool: [String: Department] = [:]
    
    
    func fetchJson(fileJson: String, nameSchool: String) {
       
        guard let url = Bundle.main.url(forResource: fileJson, withExtension: "json") else{
            fatalError("Could not find Json file")
        }
        
        let task = URLSession.shared.dataTask(with: url) { data, _,
            error in
            guard let data = data, error == nil else {
                return
            }
           
             do {
                 let listSchool = try JSONDecoder().decode([String:Department].self, from: data)
                    
                    DispatchQueue.main.async {
// I get stuck here that I don't know how to get data of one of any school
                  self.listSchool = SchoolName.nameSchool //??

                        }
                    
                } catch {
                    print(error)
                }
              
        }
        task.resume()
    }//end func fetchJson
}


0

There are 0 answers