解码 JSON Bool...我做错了什么?
Decoding JSON Bool... what am I doing wrong?
我不确定为什么没有正确解析一个变量。任何帮助将不胜感激。
String 或 [String] 的其他变量甚至其他变量都工作正常。
JSON 文件中似乎出错的部分是:
“需要”:正确
JSON 解析如下所示:
guard let FoodPrepJSON = PrepDB.parse(jsonFile: "foodPrepsDB") else {
fatalError("FoodPrepJSON URL cannot be found, or file cannot be parsed")
}
下面是相关结构。因为我必须解码成不同的子类,所以我使用了 'init' 和 'switch'。代码在底部附近中断。
struct PrepSection : Decodable {
var label : String
var key : String
var isRequired : Bool? // This is the Problem Variable
var searchKeywords : [String]?
var prepItems : [PrepItem]
enum PrepItemKey : CodingKey { // DrinksKey
case label
case key
case isRequired
case searchKeywords
case prepItems
}
enum PrepItemTypesKey : CodingKey { // Drink Prepare TypeKey
case type
}
// Set the types of prep items here
enum PrepItemTypes: String, Decodable { //DrinkTypes
case water = "water"
case beer = "beer"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: PrepItemKey.self)
// init prep items:
var prepItemsArrayForType = try container.nestedUnkeyedContainer(forKey: PrepItemKey.prepItems)
var prepItems = [PrepItem]()
var itemsArray = prepItemsArrayForType
// decode prep item and associate it with a sub-Class type
while(!prepItemsArrayForType.isAtEnd){
let item = try prepItemsArrayForType.nestedContainer(keyedBy: PrepItemTypesKey.self)
let type = try item.decode(PrepItemTypes.self, forKey: PrepItemTypesKey.type)
switch type {
case .water:
prepItems.append(try itemsArray.decode(Water.self))
case .beer:
prepItems.append(try itemsArray.decode(Beer.self))
}
}
self.prepItems = prepItems
// init label
self.label = try container.decode(String.self, forKey: .label)
// init key
self.key = try container.decode(String.self, forKey: .key)
// THIS IS THE LINE AT WHICH MY CODE BREAKS, If I exclude this line then all variables get decoded fine
self.isRequired = try container.decode(Bool.self, forKey: .isRequired)
// init keywords
self.searchKeywords = try container.decode([String].self, forKey: .searchKeywords)
}
}
将 decode
更改为 Bool
,将 decodeIfPresent
更改为 Bool?
。
我不确定为什么没有正确解析一个变量。任何帮助将不胜感激。
String 或 [String] 的其他变量甚至其他变量都工作正常。
JSON 文件中似乎出错的部分是:
“需要”:正确
JSON 解析如下所示:
guard let FoodPrepJSON = PrepDB.parse(jsonFile: "foodPrepsDB") else {
fatalError("FoodPrepJSON URL cannot be found, or file cannot be parsed")
}
下面是相关结构。因为我必须解码成不同的子类,所以我使用了 'init' 和 'switch'。代码在底部附近中断。
struct PrepSection : Decodable {
var label : String
var key : String
var isRequired : Bool? // This is the Problem Variable
var searchKeywords : [String]?
var prepItems : [PrepItem]
enum PrepItemKey : CodingKey { // DrinksKey
case label
case key
case isRequired
case searchKeywords
case prepItems
}
enum PrepItemTypesKey : CodingKey { // Drink Prepare TypeKey
case type
}
// Set the types of prep items here
enum PrepItemTypes: String, Decodable { //DrinkTypes
case water = "water"
case beer = "beer"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: PrepItemKey.self)
// init prep items:
var prepItemsArrayForType = try container.nestedUnkeyedContainer(forKey: PrepItemKey.prepItems)
var prepItems = [PrepItem]()
var itemsArray = prepItemsArrayForType
// decode prep item and associate it with a sub-Class type
while(!prepItemsArrayForType.isAtEnd){
let item = try prepItemsArrayForType.nestedContainer(keyedBy: PrepItemTypesKey.self)
let type = try item.decode(PrepItemTypes.self, forKey: PrepItemTypesKey.type)
switch type {
case .water:
prepItems.append(try itemsArray.decode(Water.self))
case .beer:
prepItems.append(try itemsArray.decode(Beer.self))
}
}
self.prepItems = prepItems
// init label
self.label = try container.decode(String.self, forKey: .label)
// init key
self.key = try container.decode(String.self, forKey: .key)
// THIS IS THE LINE AT WHICH MY CODE BREAKS, If I exclude this line then all variables get decoded fine
self.isRequired = try container.decode(Bool.self, forKey: .isRequired)
// init keywords
self.searchKeywords = try container.decode([String].self, forKey: .searchKeywords)
}
}
将 decode
更改为 Bool
,将 decodeIfPresent
更改为 Bool?
。