[Solved] How to get an string array from JSON arrays? [closed]


First of all you need to convert the raw response from the server to a Swift Dictionary.

let payload = [
  "cars": [
    [
      "color": "red",
      "model": "ferrari",
      "othersAtributes": "others atributes"
    ],
    [
      "color": "blue",
      "model": "honda",
      "othersAtributes": "others atributes"
    ],
    [
      "color": "green",
      "model": "ford",
      "othersAtributes": "others atributes"
    ],
    [
      "color": "yellow",
      "model": "porshe",
      "othersAtributes": "others atributes"
    ]
  ]
]

Then

let models: [String] = payload["cars"]!.map({ $0["model"] as! String })
print(models)

will give you ["ferrari", "honda", "ford", "porshe"].

(You might want to get replace the force unwraps ! with safer error handling mechanisms.)

5

solved How to get an string array from JSON arrays? [closed]