使用Codable使用类似的密钥解码嵌套的JSON

时间:2018-05-31 09:12:24

标签: ios json swift codable

我正在尝试解码嵌套的JSON。问题是顶层和嵌套键的名称是相似的。像:

{
    success: bool
     message: String
     error: {
      message: String
         }
} 

从后端我将收到成功消息或失败的消息。如果成功为真,则不会返回错误密钥,如果错误,则会发送错误以及消息。

所以如果成功的话:

{
    success: true
     message: "Success message"
} 

如果失败:

    {
        success: false
         error:{
              message: "Failed message"
        }
    } 

以上将是返回的json。这是我的解码结构:

struct loginResponse : Codable{
    var success: Bool
    var success_message: String
    var error_message: String


enum loginResponseKeys: String, CodingKey{
    case success
    case error
    case success_message = "message" // raw value is not unique
    case error_message = "message"
}

init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: loginResponseKeys.self)
    let error = try container.nestedContainer(keyedBy: loginResponseKeys.self, forKey: .error)
    error_message = try error.decode(String.self, forKey: .error_message)
    let message = try container.decode(String.self, forKey:.success_message)
}

正确地说,原始价值不是唯一的。但是我该如何克服呢?

3 个答案:

答案 0 :(得分:4)

您可以为ErrorMessage创建结构

struct LoginResponse: Codable {
    let success: Bool
    let message: String?
    let error: ErrorMessage?
}

struct ErrorMessage: Codable {
    let message: String?
}


extension LoginResponse {
    init(data: Data) throws {
        self = try JSONDecoder().decode(LoginResponse.self, from: data)
    }
}

假设这个Json:

{
    "success": true,
     "message": "success",
     "error": {
      "message": "Error Message"
         }
} 

答案 1 :(得分:3)

您只需要创建一个嵌套的ErrorResponse结构。使messageerror都可选,只根据success的值解码其中一个。

您还应该遵循Swift命名约定,类型名称为UpperCamelCase,变量名称为lowerCamelCase。

struct LoginResponse : Codable{
    let success: Bool
    var successMessage: String?
    var error: ErrorResponse?

    struct ErrorResponse: Codable {
        let message: String
    }


    enum LoginResponseKeys: String, CodingKey{
        case success, error, successMessage = "message"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: LoginResponseKeys.self)
        success = try container.decode(Bool.self, forKey: .success)
        if success {
            successMessage = try container.decode(String.self, forKey: .successMessage)
        } else {
            error = try container.decode(ErrorResponse.self, forKey: .error)
        }
    }
}

答案 2 :(得分:-1)

使用KeyedCodable可能会注意到您将拥有单个“message”属性,该属性将包含成功或失败的消息,具体取决于“成功”值:

struct LoginResponse: Codable, Keyedable {
    private(set) var success: Bool!
    private(set) var message: String!

    mutating func map(map: KeyMap) throws {
        try success <-> map["success"]
        let messageKey = success ? "message" : "error.message"
        try message <-> map[messageKey]
    }

    init(from decoder: Decoder) throws {
        try KeyedDecoder(with: decoder).decode(to: &self)
    }
}