gpt4 book ai didi

json - 如何在 Swift [45] 可解码协议(protocol)中解码 JSON 字典类型的属性

转载 作者:IT老高 更新时间:2023-10-28 12:43:06 26 4
gpt4 key购买 nike

假设我有 Customer 数据类型,其中包含一个 metadata 属性,该属性可以包含客户对象中的任何 JSON 字典

struct Customer {
let id: String
let email: String
let metadata: [String: Any]
}

{  
"object": "customer",
"id": "4yq6txdpfadhbaqnwp3",
"email": "john.doe@example.com",
"metadata": {
"link_id": "linked-id",
"buy_count": 4
}
}

metadata 属性可以是任意 JSON map 对象。

在我可以从 NSJSONDeserialization 的反序列化 JSON 中转换属性但使用新的 Swift 4 Decodable 协议(protocol)之前,我仍然想不出办法做到这一点.

有人知道如何在 Swift 4 中使用 Decodable 协议(protocol)实现这一点吗?

最佳答案

灵感来自 this gist我发现,我为 UnkeyedDecodingContainer 写了一些扩展和 KeyedDecodingContainer .您可以找到指向我的要点的链接 here .通过使用此代码,您现在可以解码任何 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 {
// appropriate error handling
return
}

扩展

// Inspired by https://gist.github.com/mbuchetics/c9bc6c22033014aa0c550d3b4324411a

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 {
// See if the current value in the JSON array is `null` first and prevent infite recursion with nested arrays.
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)
}
}

关于json - 如何在 Swift [45] 可解码协议(protocol)中解码 JSON 字典类型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com