0 votes

SWIFT JSON mapping

I currently have the following JSON

   {
  "headerRS": {
    "errorCode": 0, 
    "errorDesc": "", 
    "orgChannelId": "", 
    "orgChannelRef": "", 
    "orgMsgId": "", 
    "orgTransactionType": "", 
    "timestamp": ""
  }, 
  "messageRS": {
    "data": [
      {
        "fields": {
          "BranchId": "", 
          "CellPhone": "", 
          "ClientId": "", 
          "ClientType": "", 
          "DocumentId": "", 
          "DocumentType": "", 
          "LastName": " ", 
          "Mail": "", 
          "Name": "", 
          "Namec": "", 
          "Phone": ""
        }, 
        "id": 1
      }
    ], 
    "description": "NO DESCRIPTION ERROR", 
    "errorCode": "0"
  }
}

What I need is to be able to access the data within fields

I am trying as follows

 let messageRS = responseJSONDict!["messageRS"] as! [String: Any]
 if let data =  messageRS["data"]  as? [String: Any]{
 }

Even the messageRS works perfectly. But always the data returns me nil, how could I easily access to the information as I can put to the "as" to be mapped and to be able to access by keys to these values.

introducir la descripción de la imagen aquí

introducir la descripción de la imagen aquí

2voto

Luis Tejada Points 66

Data is an array of objects, not an object, so you must cast it to an array.

 let messageRS = responseJSONDict!["messageRS"] as! [String: Any]
 if let data =  messageRS["data"]  as? [[String: Any]]{
 }

2voto

kjoe Points 171

I recommend you to use codable for the JSON mapping, you will avoid a lot of problems with the JSON structure, use this link www.json4swift.com, this will generate the models when you have it where you make the request,

do{
  let response = try JSONDecoder().decode(DataResponse.self, from: data)
  let fieldsData = response.messagesRS?.data

assuming you have these Structs at least these two.

    struct DataResponse : Codable {
    let headerRS : HeaderRS?
    let messageRS : MessageRS?

    enum CodingKeys: String, CodingKey {

        case headerRS = "headerRS"
        case messageRS = "messageRS"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        headerRS = try values.decodeIfPresent(HeaderRS.self, forKey: .headerRS)
        messageRS = try values.decodeIfPresent(MessageRS.self, forKey: .messageRS)
    }
}
struct MessageRS : Codable {
    let data : [Data]?
    let description : String?
    let errorCode : String?

    enum CodingKeys: String, CodingKey {

        case data = "data"
        case description = "description"
        case errorCode = "errorCode"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        data = try values.decodeIfPresent([Data].self, forKey: .data)
        description = try values.decodeIfPresent(String.self, forKey: .description)
        errorCode = try values.decodeIfPresent(String.self, forKey: .errorCode)
    }

}

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X