Home > database >  Swift and Json array of dictionary
Swift and Json array of dictionary

Time:09-14

What is the right json to represent an array of dictionary plz

I have this struct in Swift

struct myResults: Codable {
    let type: String
    let results: [[String: String]]
}

I tried this json representation

{
   "type":"express",
   "results":[
  { "name": "Name1", "result": "Result1" },
  { "name": "Name2", "result": "Result2" },
  { "name": "Name3", "result": "Result3" }
]
 }

but it seems that the json is representing the field results as results: [String] but I want to have json that represents let results: [[String: String]]

So I'm confused about what to write as a json to represent let results: [[String: String]]

Any help/idea plz ? Thank you enter image description here

CodePudding user response:

You can access the value through the index so use like results: [String] or better to use your model to get the string values.

CodePudding user response:

The right answer

{
   "type":"express",
   "results":[
  { "name": "Name1", "result": "Result1" },
  { "name": "Name2", "result": "Result2" },
  { "name": "Name3", "result": "Result3" }
]
 }
  • Related