簡體   English   中英

使用解碼協議(可編碼)解析 Json

[英]Parsing Json with Decoding Protocol (Codable)

我正在嘗試使用Codable協議解析 JSON 但出現異常。 此異常是由於 null 值響應第二個響應的嵌套鍵容器。

它在 json 響應下工作正常

    let jsonStr = """
   {
     "response": {
         "PURCHASE_FREE_RESPONSE": {
                                "TOPUP": null,
                                "TOTAL_PAYMENT_AMOUNT": "0",
                                "TAX": null,
                                "VAT": null,
                               ........
                                 "CUSTOM_FIELD_2": null,
                                 "CUSTOM_FIELD_3": null,
                                 "FEE": null
                  }
         },
     "API-Code": "ttttt",
     "statuscode": 200,
     "statusMessage": "SUCCESS",
     "respCode": {
     "ERROR_CODE": "0",
     "ERROR_DESC": "Success"
      }
     }
    """
    //=========Second Response===============

        let jsonStr = """
         {
            **"response": null,**
            "API-Code": null,
            "statuscode": 500,
            "statusMessage": "Undefined index: HOUSE_NO",
              **"respCode": null**
           }
 
        """

我的 Model 看起來像:-

struct SimPurResponse:Decodable{
let topUp:String
let totalPaymentAmt:String
let transId:String


let apiCode:String
let statuscode:Int
let statusMessage:String

let errorCode:String
let errorDesc:String


private enum ResponseOuter : String, CodingKey {
    case response
    case apiCode      = "API-Code"
    case statuscode   = "statuscode"
    case respCode
    case statusMessage = "statusMessage"
}

enum RespCodeKeys : String, CodingKey {
    case errorCode   = "ERROR_CODE"
    case errorDesc   = "ERROR_DESC"
}

private enum Response : String, CodingKey {
    case PURCHASE_FREE_SIM_RESPONSE
}

private enum purchaseFreeSimResInfo : String, CodingKey {
    case topUp            = "TOPUP"
    case totalPaymentAmt  = "TOTAL_PAYMENT_AMOUNT"
    case transId          = "TRANSACTION_ID"
    case tax              = "TAX"
    case vat              =  "VAT"
    case shipingAmt       = "SHIPPING_AMOUNT"
    
}


init(from decoder :Decoder) throws {
    let container     = try decoder.container(keyedBy: ResponseOuter.self)
    self.apiCode      = try container.decodeIfPresent(String.self, forKey: .apiCode) ?? ""
    self.statuscode    = try container.decodeIfPresent(Int.self, forKey: .statuscode) ?? 600
    self.statusMessage = try container.decodeIfPresent(String.self, forKey: .statusMessage) ?? ""
    
    let respCodeCont =  try container.nestedContainer(keyedBy: RespCodeKeys.self, forKey: .respCode)
    self.errorCode   =  try respCodeCont.decodeIfPresent(String.self, forKey: .errorCode) ?? ""
    self.errorDesc   =  try respCodeCont.decodeIfPresent(String.self, forKey: .errorDesc) ?? ""
    
   
    let purcCont          = try container.nestedContainer(keyedBy: Response.self, forKey: .response)
    let purSimCont        = try purcCont.nestedContainer(keyedBy: purchaseFreeSimResInfo.self, forKey: .PURCHASE_FREE_SIM_RESPONSE)

      self.topUp            = try purSimCont?.decodeIfPresent(String.self,  forKey: .topUp) ?? ""
     self.totalPaymentAmt    = try purSimCont?.decodeIfPresent(String.self, forKey: .totalPaymentAmt) ?? ""
     self.transId           = try purSimCont?.decodeIfPresent(String.self, forKey: .transId) ?? ""
     self.tax              = try purSimCont?.decodeIfPresent(String.self, forKey: .tax) ?? ""
     self.vat               = try purSimCont?.decodeIfPresent(String.self, forKey: .vat) ?? ""
     self.shipingAmt       = try purSimCont?.decodeIfPresent(String.self, forKey: .shipingAmt) ?? ""
    
}

}

        let dataJosn = Data(jsonStr.utf8)

 do {
     let simDetail = try JSONDecoder().decode(SimPurResponse.self, from: dataJosn)

   print(simDetail)


    }catch let error {
      print(error)
    }

我在處理這兩種情況時遇到問題。 以上完整代碼

只需將您的“嘗試”更改為“嘗試”? 在解碼器初始化程序塊中訪問 nestedContainer 時,並在需要時添加可選鏈接:

init(from decoder :Decoder) throws {

    let data = try decoder.container(keyedBy: ResponseOuter.self)

    self.apiCode      = try data.decodeIfPresent(String.self, forKey: .apiCode) ?? ""
    self.statuscode    = try data.decodeIfPresent(Int.self, forKey: .statuscode) ?? 600
    self.statusMessage = try data.decodeIfPresent(String.self, forKey: .statusMessage) ?? ""

    let respCodeCont =  try? data.nestedContainer(keyedBy: RespCodeKeys.self, forKey: .respCode)
    self.errorCode   =  try respCodeCont?.decodeIfPresent(String.self, forKey: .errorCode) ?? ""
    self.errorDesc   =  try respCodeCont?.decodeIfPresent(String.self, forKey: .errorDesc) ?? ""

    let purcCont          = try? data.nestedContainer(keyedBy: Response.self, forKey: .response)
    let purSimCont        = try purcCont?.nestedContainer(keyedBy: purchaseFreeSimResInfo.self, forKey: .PURCHASE_FREE_SIM_RESPONSE)
    self.topUp            = try purSimCont?.decodeIfPresent(String.self, forKey: .topUp) ?? ""
    self.totalPaymentAmt    = try purSimCont?.decodeIfPresent(String.self, forKey: .totalPaymentAmt) ?? ""
    self.transId           = try purSimCont?.decodeIfPresent(String.self, forKey: .transId) ?? ""
    self.tax              = try purSimCont?.decodeIfPresent(String.self, forKey: .tax) ?? ""
    self.vat               = try purSimCont?.decodeIfPresent(String.self, forKey: .vat) ?? ""
    self.shipingAmt       = try purSimCont?.decodeIfPresent(String.self, forKey: .shipingAmt) ?? ""

    self.itgTransId          = try purSimCont?.decodeIfPresent(String.self, forKey: .itgTransId) ?? ""
    self.chanelTransId       = try purSimCont?.decodeIfPresent(String.self, forKey: .chanelTransId) ?? ""
    self.paymentResponse     = try purSimCont?.decodeIfPresent(String.self, forKey: .paymentResponse) ?? ""
    self.apmResponse        = try purSimCont?.decodeIfPresent(String.self, forKey: .apmResponse) ?? ""
    self.applicationType    = try purSimCont?.decodeIfPresent(String.self, forKey: .applicationType) ?? ""
    self.nameorPageId       = try purSimCont?.decodeIfPresent(String.self, forKey: .nameorPageId) ?? ""
    self.url                = try purSimCont?.decodeIfPresent(String.self, forKey: .url) ?? ""

    self.customField1       = try purSimCont?.decodeIfPresent(String.self, forKey: .customField1) ?? ""
    self.customField2       = try purSimCont?.decodeIfPresent(String.self, forKey: .customField2) ?? ""
    self.customField3       = try purSimCont?.decodeIfPresent(String.self, forKey: .customField3) ?? ""
    self.fee                 = try purSimCont?.decodeIfPresent(String.self, forKey: .fee) ?? ""
}

希望這可以幫助。 干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM