简体   繁体   English

swift 将字典转换为 jsonString 错误:协议类型 'Any' 不能符合 'Encodable' 因为只有具体类型才能符合协议

[英]swift Convert dictionary to jsonString error : Protocol type 'Any' cannot conform to 'Encodable' because only concrete types can conform to protocols

I have a dictionary and I want to convert to jsonstring.我有一本字典,我想转换为 jsonstring。

Protocol type 'Any' cannot conform to 'Encodable' because only concrete types can conform to protocols How to fix it?协议类型 'Any' 不能符合 'Encodable' 因为只有具体类型才能符合协议 如何解决? Thanks.谢谢。

func save(body: [String: Any]) -> Void {

    let encoder = JSONEncoder()
    if let jsonData = try? encoder.encode(body) { //error here.
        if let jsonString = String(data: jsonData, encoding: .utf8) {
            print(jsonString)
        }
    }
}

You need to give the body type something that conforms to Codable .你需要给身体类型一些符合Codable的东西。 To fix this issue create another struct that conforms to Codable and change the type of body variable to it.要解决此问题,请创建另一个符合struct的结构Codable body变量的类型更改为它。

Here's an example:这是一个例子:

struct Body: Codable { 
// all the properties you require can be added here.
}

func save(body: Body) -> Void {

    let encoder = JSONEncoder()
    if let jsonData = try? encoder.encode(body) {
        if let jsonString = String(data: jsonData, encoding: .utf8) {
            print(jsonString)
        }
    }
}

Or you can use JSONSerialisation like this:或者您可以像这样使用 JSONSerialisation:

func save(body: [String: Any]) -> Void {

    if let jsonData = try? JSONSerialization.data(withJSONObject: body, options: .prettyPrinted) {
        if let jsonString = String(data: jsonData, encoding: .utf8) {
            print(jsonString)
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 协议类型不能符合协议,因为只有具体类型才能符合协议 - Protocol type cannot conform to protocol because only concrete types can conform to protocols 协议类型“Encodable”的值不能符合“Encodable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols 协议类型“Any”的值不能符合“Equatable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols Swift 错误:“类型 '()' 不能符合 'View';只有 struct/enum/class 类型可以符合协议”调用函数写入文本时 - Swift error: "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols" when calling function to write text 类型 '()' 不能符合 'View'; 只有 struct/enum/class 类型才能符合协议; 使用 SwiftUI 调用函数 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols; calling functions with SwiftUI navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型“ MyWeather”不符合协议“可编码”错误 - Type 'MyWeather' does not conform to protocol 'Encodable' error 我收到错误“类型'(UITextRange)->字符串?' 不能符合“BinaryInteger”; 只有结构/枚举/类类型可以符合协议” - I got error “Type '(UITextRange) -> String?' cannot conform to 'BinaryInteger'; only struct/enum/class types can conform to protocol” 类型不符合“可编码”协议 - Type does not conform to protocol 'Encodable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM