Use an umbrella struct
struct Root : Decodable {
let shifts : [Shift]
let prices : [Price]
}
and two different structs for shifts and prices:
struct Shift : Decodable {
let id: Int
let region, city, nationality : String
let idService : Int
let shiftDate, shiftType, weekday : String
let quantityStaff, leadHours : Int
let createdAt, updatedAt : String
let deletedAt : String?
}
struct Price : Decodable {
let id, idService : Int
let nationality : String
let price : Int
let createdAt, updatedAt : String
let deletedAt : String?
}
To decode the JSON write
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder .decode(Root.self, from: responseData)
print(result.prices)
print(result.shifts)
} catch { print(error) }
Further I recommend to decode keys like shiftType
and weekday
directly into an enum
for example
enum ShiftType : String, Decodable {
case day, night
}
struct Shift : Decodable {
let id: Int
...
let shiftType : ShiftType
...
0
solved parsing two different object’s array from JSON response