将嵌套的可编码对象转换为字典 swift
Converting nested codable objects to dictionaries swift
我目前正在使用此扩展程序,它适用于基本对象。我很难弄清楚如何通过此调用将嵌套的 Codable 对象转换为字典...提前致谢!
public extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { [=11=] as? [String: Any] }
}
}
我不确定嵌套词典是什么意思,但看起来您不想 return 词典数组。我认为您正在寻找的是一本字典,其值是字典。不管怎样,我都会 post 两种选择:
extension Encodable {
// this would try to encode an encodable type and decode it into an a dictionary
var dictionary: [String: Any] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:]
}
// this would try to encode an encodable type and decode it into an array of dictionaries
var dictionaries: [[String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [] }
return (try? JSONSerialization.jsonObject(with: data)) as? [[String: Any]] ?? []
}
// this would try to encode an encodable type and decode it into a dictionary with dictionaries as its value
var nestedDictionaries: [String: [String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: [String: Any]] ?? [:]
}
// this will return only the properties that are referring to a nested structure/classe
var nestedDictionariesOnly: [String: [String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return ((try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:]).compactMapValues { [=10=] as? [String:Any] }
}
}
我目前正在使用此扩展程序,它适用于基本对象。我很难弄清楚如何通过此调用将嵌套的 Codable 对象转换为字典...提前致谢!
public extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { [=11=] as? [String: Any] }
}
}
我不确定嵌套词典是什么意思,但看起来您不想 return 词典数组。我认为您正在寻找的是一本字典,其值是字典。不管怎样,我都会 post 两种选择:
extension Encodable {
// this would try to encode an encodable type and decode it into an a dictionary
var dictionary: [String: Any] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:]
}
// this would try to encode an encodable type and decode it into an array of dictionaries
var dictionaries: [[String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [] }
return (try? JSONSerialization.jsonObject(with: data)) as? [[String: Any]] ?? []
}
// this would try to encode an encodable type and decode it into a dictionary with dictionaries as its value
var nestedDictionaries: [String: [String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return (try? JSONSerialization.jsonObject(with: data)) as? [String: [String: Any]] ?? [:]
}
// this will return only the properties that are referring to a nested structure/classe
var nestedDictionariesOnly: [String: [String: Any]] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return ((try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:]).compactMapValues { [=10=] as? [String:Any] }
}
}