Use the below struct for unwrapping the data
// MARK: - Response
struct Response: Codable {
let dog, pitbull: Dog?
enum CodingKeys:String, CodingKey {
case dog
case pitbull
}
init(from decoder: any Decoder) throws {
let container = try? decoder.container(keyedBy: CodingKeys.self)
dog = try? container?.decodeIfPresent(Dog.self, forKey: .dog)
pitbull = try? container?.decodeIfPresent(Dog.self, forKey: .pitbull)
}
}
// MARK: - Dog
struct Dog: Codable {
let type, logoLocation: String?
enum CodingKeys:String, CodingKey {
case type
case logoLocation
}
init(from decoder: any Decoder) throws {
let container = try? decoder.container(keyedBy: CodingKeys.self)
type = try? container?.decodeIfPresent(String.self, forKey: .type)
logoLocation = try? container?.decodeIfPresent(String.self, forKey: .logoLocation)
}
}
Use the below code for decode the json Data
do {
let dictonary = try JSONDecoder().decode([String:Dog].self,from:data)
// Or use this code
let response = try JSONDecoder().decode(Response.self,from:data)
}
catch let error{
print(error)
}