String:Any 的字典不符合协议 'Decodable'
Dictionary of String:Any does not conform to protocol 'Decodable'
我正在尝试实现 Decodable 来解析 json 请求,但是 json 请求在对象内部有一个字典。
这是我的代码:
struct myStruct : Decodable {
let content: [String: Any]
}
enum CodingKeys: String, CodingKey {
case content = "content"
}
但是我收到这个错误:
类型'MyClass.myStruct'不符合协议'Decodable'
如何在不出现此错误的情况下将变量声明为字典?
非常感谢你的帮助
您目前无法使用 Swift 编码框架解码 [String: Any]
。如果您需要解码 [String: Any]
,则需要使用较低级别的反序列化策略并“手动”解码。例如,如果您的输入是 JSON,您可以使用 Foundation 的 JSONSerialization
或像 SwiftyJSON.
这样的第三方库
Swift Evolution 上已经讨论过这个问题:“Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4”. Apple's main Coding/Codable programmer, Itai Ferber, has been involved in the discussion and is interested in providing a solution, but it is unlikely to happen for Swift 5(可能会在 WWDC 2018 上宣布,并在 September/October 2018 年左右完成)。
您可以将 JSONDecoder
(它是开源的)的实现复制到您的项目中并修改它以添加获得未评估的 [String: Any]
的能力。 Itai 在我上面链接的线程中讨论了所需的修改。
好吧...从技术上讲,您可以这样做,但它需要您使用第三方组件 SwiftyJSON 来表示字典。
此外,我假设您这样做是因为 content
可能有非规范化数据,并且您有意将其视为字典。
既然如此,那就继续吧:
import SwiftyJSON
struct MyStruct : Decodable {
//... your other Decodable objects like
var name: String
//the [String:Any] object
var content: JSON
}
此处,JSON
是将代表您的字典的 SwiftyJSON 对象。事实上,它也可以代表一个数组。
工作示例:
let jsonData = """
{
"name": "Swifty",
"content": {
"id": 1,
"color": "blue",
"status": true,
"details": {
"array" : [1,2,3],
"color" : "red"
}
}
}
""".data(using: .utf8)!
do {
let test = try JSONDecoder().decode(MyStruct.self,
from: jsonData)
print(test)
}
catch {
print(error)
}
我正在尝试实现 Decodable 来解析 json 请求,但是 json 请求在对象内部有一个字典。
这是我的代码:
struct myStruct : Decodable {
let content: [String: Any]
}
enum CodingKeys: String, CodingKey {
case content = "content"
}
但是我收到这个错误:
类型'MyClass.myStruct'不符合协议'Decodable'
如何在不出现此错误的情况下将变量声明为字典?
非常感谢你的帮助
您目前无法使用 Swift 编码框架解码 [String: Any]
。如果您需要解码 [String: Any]
,则需要使用较低级别的反序列化策略并“手动”解码。例如,如果您的输入是 JSON,您可以使用 Foundation 的 JSONSerialization
或像 SwiftyJSON.
Swift Evolution 上已经讨论过这个问题:“Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4”. Apple's main Coding/Codable programmer, Itai Ferber, has been involved in the discussion and is interested in providing a solution, but it is unlikely to happen for Swift 5(可能会在 WWDC 2018 上宣布,并在 September/October 2018 年左右完成)。
您可以将 JSONDecoder
(它是开源的)的实现复制到您的项目中并修改它以添加获得未评估的 [String: Any]
的能力。 Itai 在我上面链接的线程中讨论了所需的修改。
好吧...从技术上讲,您可以这样做,但它需要您使用第三方组件 SwiftyJSON 来表示字典。
此外,我假设您这样做是因为 content
可能有非规范化数据,并且您有意将其视为字典。
既然如此,那就继续吧:
import SwiftyJSON
struct MyStruct : Decodable {
//... your other Decodable objects like
var name: String
//the [String:Any] object
var content: JSON
}
此处,JSON
是将代表您的字典的 SwiftyJSON 对象。事实上,它也可以代表一个数组。
工作示例:
let jsonData = """
{
"name": "Swifty",
"content": {
"id": 1,
"color": "blue",
"status": true,
"details": {
"array" : [1,2,3],
"color" : "red"
}
}
}
""".data(using: .utf8)!
do {
let test = try JSONDecoder().decode(MyStruct.self,
from: jsonData)
print(test)
}
catch {
print(error)
}