Faced the problem that I can not find the structure for decoding this JSON:

"cars": [ { "name":"Ford", "models": "Fiesta", "color": "red" }, { "name":"BMW", "models": "X5", "color": "green" }, { "name":"Fiat", "models": "500", "color": "blue" } ] 

In the JSON array itself there will not be an exact number of elements (there are 3 of them). And how, after decoding, will it be possible to access the elements of the extracted array?

    1 answer 1

    JSON is not valid. The correct JSON structure must begin and end with curly braces.

     var str = """ {"cars": [ { "name":"Ford", "models": "Fiesta", "color": "red" }, { "name":"BMW", "models": "X5", "color": "green" }, { "name":"Fiat", "models": "500", "color": "blue" } ]} """ struct Response: Codable { let cars: [Car] } struct Car: Codable { let name: String let models: String let color: String } let data = try JSONDecoder().decode(Response.self, from: str.data(using: .utf8)!) 
    • Thanks, now everything works :) - LoonyMan