Search code examples
jsonswiftjsondecoder

How to define list of dictionaries in a struct


I want to load JSON that contains a list of dictionary, for example

{error: false, "objects": [{"id": 1, "name": "cat"}, {"id": 2, "name": "dog"}, {"id": 3, "name": "fish"}]

How could this be defined in a

struct name: Codable, Identifiable  {
}

to be used in JSONDecoder to decode the received data.
How can the struct be written to define this datatype


Solution

  • struct data: Codable {
       let error: Bool
       let objects: [dataObjects]
    }
    
    struct dataObjects: Codable {
        let id: Int
        let name: String
    }