I\'ve a JSON like this.
I need to make a corresponding Decodable struct in my iOS app using Swift 4.
{
\"cherry\": {
\"filling\": \"cherrie
What you need is to get creative in defining the CodingKeys
. Let's call the response a FoodList
and the inner structure FoodDetail
. You haven't defined the properties of FoodDetail
so I assume that the keys are all optional.
struct FoodDetail: Decodable {
var name: String!
var filling: String?
var goodWithIceCream: Bool?
var madeBy: String?
var flavor: String?
var forABirthday: Bool?
enum CodingKeys: String, CodingKey {
case filling, goodWithIceCream, madeBy, flavor, forABirthday
}
}
struct FoodList: Decodable {
var foodNames: [String]
var foodDetails: [FoodDetail]
// This is a dummy struct as we only use it to satisfy the container(keyedBy: ) function
private struct CodingKeys: CodingKey {
var intValue: Int?
var stringValue: String
init?(intValue: Int) { self.intValue = intValue; self.stringValue = "" }
init?(stringValue: String) { self.stringValue = stringValue }
}
init(from decoder: Decoder) throws {
self.foodNames = [String]()
self.foodDetails = [FoodDetail]()
let container = try decoder.container(keyedBy: CodingKeys.self)
for key in container.allKeys {
let foodName = key.stringValue
var foodDetail = try container.decode(FoodDetail.self, forKey: key)
foodDetail.name = foodName
self.foodNames.append(foodName)
self.foodDetails.append(foodDetail)
}
}
}
// Usage
let list = try! JSONDecoder().decode(FoodList.self, from: jsonData)