How to write a Decodable for a JSON in Swift 4, where keys are dynamic?

后端 未结 1 1282
借酒劲吻你
借酒劲吻你 2021-02-06 15:20

I\'ve a JSON like this.

I need to make a corresponding Decodable struct in my iOS app using Swift 4.

{
    \"cherry\": {
        \"filling\": \"cherrie         


        
相关标签:
1条回答
  • 2021-02-06 15:39

    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)
    
    0 讨论(0)
提交回复
热议问题