blackstarshop.ru/index.php?route=api/v1/categories
{"id":67,"iconImage":"image\/catalog\/style\/modile\/icons-03.png"
id имеет целочисленный тип, хотя во всех остальных местах это строка. struct EntryList: Decodable {
struct DynamicCodingKey: CodingKey {
var stringValue: String
init?(stringValue: String) { self.stringValue = stringValue }
var intValue: Int? { nil }
init?(intValue: Int) { nil }
}
struct Entry: Decodable {
struct Content: Decodable {
struct Subcategory: Decodable {
let id: String
let iconImage: String
let name: String
let sortOrder: String
let type: String
}
let iconImage: String
let iconImageActive: String
let image: String
let name: String
let sortOrder: String
let subcategories: [Subcategory]
}
let name: String
let content: Content
}
let entries: [Entry]
init(from decoder: Decoder) throws {
let entriesContainer = try decoder.container(keyedBy: DynamicCodingKey.self)
entries = try entriesContainer.allKeys.map { key in
print(key)
let content = try entriesContainer.decode(Entry.Content.self, forKey: key)
return Entry(name: key.stringValue, content: content)
}
}
}
var entryList: EntryList?
let task = URLSession
.shared
.dataTask(with: URL(string: "https://blackstarshop.ru/index.php?route=api/v1/categories")!) { (data, _, error) in
guard error == nil else { return }
guard let data = data else { return }
entryList = try! JSONDecoder().decode(EntryList.self, from: data)
}
task.resume()