受到我在这个代码片段的启发,我为UnkeyedDecodingContainer
和KeyedDecodingContainer
编写了一些扩展。你可以在这里找到我的代码片段链接。使用此代码,您现在可以使用熟悉的语法解码任何Array<Any>
或Dictionary<String, Any>
:
let dictionary: [String: Any] = try container.decode([String: Any].self, forKey: key)
或者
let array: [Any] = try container.decode([Any].self, forKey: key)
编辑: 我发现有一个限制,即解码字典数组[[String: Any]]
所需的语法如下。你可能想抛出一个错误而不是强制转换:
let items: [[String: Any]] = try container.decode(Array<Any>.self, forKey: .items) as! [[String: Any]]
编辑2: 如果你只是想将整个文件转换为字典,最好使用JSONSerialization的API,因为我尚未找到一种扩展JSONDecoder本身以直接解码字典的方法。
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
return
}
扩展功能
struct JSONCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
self.init(stringValue: "\(intValue)")
self.intValue = intValue
}
}
extension KeyedDecodingContainer {
func decode(_ type: Dictionary<String, Any>.Type, forKey key: K) throws -> Dictionary<String, Any> {
let container = try self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key)
return try container.decode(type)
}
func decodeIfPresent(_ type: Dictionary<String, Any>.Type, forKey key: K) throws -> Dictionary<String, Any>? {
guard contains(key) else {
return nil
}
guard try decodeNil(forKey: key) == false else {
return nil
}
return try decode(type, forKey: key)
}
func decode(_ type: Array<Any>.Type, forKey key: K) throws -> Array<Any> {
var container = try self.nestedUnkeyedContainer(forKey: key)
return try container.decode(type)
}
func decodeIfPresent(_ type: Array<Any>.Type, forKey key: K) throws -> Array<Any>? {
guard contains(key) else {
return nil
}
guard try decodeNil(forKey: key) == false else {
return nil
}
return try decode(type, forKey: key)
}
func decode(_ type: Dictionary<String, Any>.Type) throws -> Dictionary<String, Any> {
var dictionary = Dictionary<String, Any>()
for key in allKeys {
if let boolValue = try? decode(Bool.self, forKey: key) {
dictionary[key.stringValue] = boolValue
} else if let stringValue = try? decode(String.self, forKey: key) {
dictionary[key.stringValue] = stringValue
} else if let intValue = try? decode(Int.self, forKey: key) {
dictionary[key.stringValue] = intValue
} else if let doubleValue = try? decode(Double.self, forKey: key) {
dictionary[key.stringValue] = doubleValue
} else if let nestedDictionary = try? decode(Dictionary<String, Any>.self, forKey: key) {
dictionary[key.stringValue] = nestedDictionary
} else if let nestedArray = try? decode(Array<Any>.self, forKey: key) {
dictionary[key.stringValue] = nestedArray
}
}
return dictionary
}
}
extension UnkeyedDecodingContainer {
mutating func decode(_ type: Array<Any>.Type) throws -> Array<Any> {
var array: [Any] = []
while isAtEnd == false {
if try decodeNil() {
continue
} else if let value = try? decode(Bool.self) {
array.append(value)
} else if let value = try? decode(Double.self) {
array.append(value)
} else if let value = try? decode(String.self) {
array.append(value)
} else if let nestedDictionary = try? decode(Dictionary<String, Any>.self) {
array.append(nestedDictionary)
} else if let nestedArray = try? decode(Array<Any>.self) {
array.append(nestedArray)
}
}
return array
}
mutating func decode(_ type: Dictionary<String, Any>.Type) throws -> Dictionary<String, Any> {
let nestedContainer = try self.nestedContainer(keyedBy: JSONCodingKeys.self)
return try nestedContainer.decode(type)
}
}
UnkeyedDecodingContainer
的decode(_ type: Array<Any>.Type) throws -> Array<Any>
中的最后一个条件是检查 嵌套 数组。因此,如果您有一个类似以下结构的数据:[true, 452.0, ["a", "b", "c"]]
它将提取嵌套的["a", "b", "c"]
数组。UnkeyedDecodingContainer
的decode
方法会从容器中“弹出”元素。这不应该导致无限递归。 - loudmouth{"array": null}
。因此,你的guard contains(key)
可能会通过,但是在几行后,当尝试解码 "array" 键的 null 值时,它将崩溃。因此,在调用decode
之前最好添加一个条件来检查该值是否实际上不是 null。 - chebur} else if let nestedArray = try? decode(Array<Any>.self, forKey: key)
,而是尝试使用} else if var nestedContainer = try? nestedUnkeyedContainer(), let nestedArray = try? nestedContainer.decode(Array<Any>.self)
。 - Jon Brooks