简体   繁体   English

Swift Codable 嵌套 json 作为字符串

[英]Swift Codable with nested json as string

The services I use for backend calls returns all this json-structure:我用于后端调用的服务返回所有这些 json 结构:

{
    "status" : "OK",
    "payload" : **something**
}

Where something can be a simple string:有些东西可以是一个简单的字符串:

{
    "status" : "OK",
    "payload" : "nothing changed"
}

or a nested json (any json with any properties), for example:或嵌套的 json(具有任何属性的任何 json),例如:

{
    "status" : "OK",
    "payload" : {
                    "someInt" : 2,
                    "someString" : "hi",
                    ...
                }
}

This is my struct:这是我的结构:

struct GenericResponseModel: Codable {

    let status:String?
    let payload:String?
}

I want to decode "payload" always as a string .我想始终将“有效负载”解码为字符串 So in the second case I want that the payload property of my "GenericResponseModel" contains the json string of that field, but If I try to decode that response I get the error:因此,在第二种情况下,我希望“GenericResponseModel”的有效负载属性包含该字段的 json 字符串,但是如果我尝试解码该响应,则会收到错误消息:

Type 'String' mismatch: Expected to decode String but found a dictionary instead

Is possible to archive what I want?可以存档我想要的吗?

Many thanks非常感谢

How about this…这个怎么样…

Declare a PayloadType protocol…声明一个PayloadType协议......

protocol PayloadType: Decodable { }

and make String , and struct Payload conform to it…并使Stringstruct Payload符合它......

extension String: PayloadType { }

struct Payload: Decodable, PayloadType {
    let someInt: Int
    let someString: String
}

Then make GenericResponseModel generic…然后使GenericResponseModel泛型......

struct GenericResponseModel<T: PayloadType>: Decodable {

    let status: String
    let payload: T

    enum CodingKeys: CodingKey {
        case status, payload
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        status = try container.decode(String.self, forKey: .status)            
        payload = try container.decode(T.self, forKey: .payload)
    }
}

Then you can decode as follows…然后你可以解码如下...

let data = """
{
"status" : "OK",
"payload" : "nothing changed"
}
""".data(using: .utf8)!

print(try JSONDecoder().decode(GenericResponseModel<String>.self, from: data))

// GenericResponseModel<String>(status: "OK", payload: "nothing changed")

and

let data2 = """
{
"status" : "OK",
"payload" : {
"someInt" : 2,
"someString" : "hi"
}
}
""".data(using: .utf8)!

print(try JSONDecoder().decode(GenericResponseModel<Payload>.self, from: data2))

// GenericResponseModel<Payload>(status: "OK", payload: Payload(someInt: 2, someString: "hi"))


Of course, this relies on you knowing the payload type in advance.当然,这取决于您事先了解payload类型。 You could get around this by throwing a specific error if payload is the wrong type…如果有效负载类型错误,您可以通过抛出特定错误来解决此问题……

enum GenericResponseModelError: Error {
    case wrongPayloadType
}

and then…进而…

init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    status = try container.decode(String.self, forKey: .status)

    do {
        payload = try container.decode(T.self, forKey: .payload)
    } catch {
        throw GenericResponseModelError.wrongPayloadType
    }
}

Then handle this error when you decode…然后在解码时处理这个错误......

let data = """
{
"status" : "OK",
"payload" : {
"someInt" : 2,
"someString" : "hi"
}
}
""".data(using: .utf8)!

do {
    let response = try JSONDecoder().decode(GenericResponseModel<String>.self, from: data) // Throws
    print(response) 
} catch let error as GenericResponseModelError where error == .wrongPayloadType {
    let response = try JSONDecoder().decode(GenericResponseModel<Payload>.self, from: data2) // Success!
    print(response)
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM