Swift: How to decode result from api which is being returned as a string or as a string of array?

末鹿安然 提交于 2019-12-08 10:58:19

问题


I am using NSUrlConnection and Codable library of Apple.

I am using a web API to signup new users for my application. The API returns a status and a message (in json which i map to a model).

This is my model class:

Struct SignUpResult {
  let message: String
  let status: String
} 

Struct SignUpParams {
 let name: String
 let email: String
 let mobile_no: String
 let password: String
}

If the user gave all the parameters correctly then the message is returned as a string. Like this:

{
    "status": "OK",
    "message": "User signup successfully"
}

On the other hand, if the user entered the parameters incorrectly then the message is returned as an array. Like this:

{
    "status": "INVALID_PARAMS",
    "message": [
        "The name may only contain letters."
    ]
}

If the parameters are incorrect then i get the error

"expected to decode a string but found an array instead". 

This is the code i get the error on:

let result = JSONDecoder().decode(SignUpResult.self, from: data)

What should i do?


回答1:


My suggestion is to decode status as enum and conditionally decode message as String or [String]. messages is declared as array.

enum SignUpStatus : String, Decodable {
    case success = "OK", failure = "INVALID_PARAMS"
}

struct SignUpResult : Decodable {
    let messages : [String]
    let status: SignUpStatus

    private enum CodingKeys : String, CodingKey { case status, message }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        status = try container.decode(SignUpStatus.self, forKey: .status)
        switch status {
        case .success: messages = [try container.decode(String.self, forKey: .message)]
        case .failure: messages = try container.decode([String].self, forKey: .message)
        }
    }
}


来源:https://stackoverflow.com/questions/51711891/swift-how-to-decode-result-from-api-which-is-being-returned-as-a-string-or-as-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!