如何在Swift 4中为JSON编写一个Decodable,其中键是动态的?

时间:2017-09-08 08:20:13

标签: ios json swift swift4 decodable

我是这样的JSON。

我需要使用Swift 4在我的iOS应用程序中创建相应的Decodable结构。

{
    "cherry": {
        "filling": "cherries and love",
        "goodWithIceCream": true,
        "madeBy": "my grandmother"
     },
     "odd": {
         "filling": "rocks, I think?",
         "goodWithIceCream": false,
         "madeBy": "a child, maybe?"
     },
     "super-chocolate": {
         "flavor": "german chocolate with chocolate shavings",
         "forABirthday": false,
         "madeBy": "the charming bakery up the street"
     }
}

需要帮助制作可解码结构。如何提及cherryoddsuper-chocolate等未知密钥。

1 个答案:

答案 0 :(得分:7)

您需要的是在定义CodingKeys时发挥创意。我们将响应称为FoodList和内部结构FoodDetail。您尚未定义FoodDetail的属性,因此我假设这些键都是可选的。

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)