{
"responseBody": {
"table": {
"data": [
[
"Forth Record",
null,
0,
"2018-08-23T18:30:01.000+0000",
0,
0,
"HCL",
"b74d10ef4fe246948cd036071787ff25"
],
[
"Third Record",
"Testing custom object record 3",
348,
"2018-08-22T18:30:01.000+0000",
36.45,
4545.45,
"HCL",
"139fdba94bb143849fef220f105d66d0"
],
[
"Second Record",
"Testing custom object record 2",
56,
"2018-08-22T18:30:01.000+0000",
6577.67,
567.67,
"HAL",
"606a06c93ea2473fb832e5daafa02df9"
],
[
"First Record",
"Testing custom object record",
75,
"2018-08-22T18:30:01.000+0000",
47.54,
67.63,
"HBL",
"29c4125f3fa947b9b252318305e986c7"
]
]
}
}
}
I want to parse above JSON
using swift 4 Codable
. Please see my objects hierarchy below
//ViewRecordResponse.swift
import Foundation
struct ViewRecordResponse : Codable {
let responseBody : ViewRecord?
enum CodingKeys: String, CodingKey {
case responseBody = "responseBody"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
responseBody = try values.decodeIfPresent(ViewRecord.self, forKey: .responseBody)
}
}
//ViewRecord.swift
import Foundation
struct ViewRecord : Codable {
let table : Table?
enum CodingKeys: String, CodingKey {
case table = "table"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
table = try values.decodeIfPresent(Table.self, forKey: .table)
}
}
//Table.swift
import Foundation
struct Table : Codable {
let data : [[String?]]?
enum CodingKeys: String, CodingKey {
case data = "data"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
data = try values.decodeIfPresent([[String?]].self, forKey: .data)
}
}
but when I try to decode the JSON using Codeable Mapping I got an error saying
The data couldn't be read because it is missing.
The data couldn’t be read because it isn’t in the correct format.
code for decode to JSON object
do {
let jsonDecoder = JSONDecoder()
let response = try jsonDecoder.decode(ViewRecordResponse.self, from: data)
} catch let error {
print(error.localizedDescription)
}
Edit 1 - My Data value
Printing description of data:
▿ 557 bytes
- count : 557
▿ pointer : 0x0000000104a23005
- pointerValue : 4372705285
Edit 2 - data objects not follow any specific pattern issue
"data": [
[
456,
31.04,
10000,
"Dummy Data",
"text area dummy",
"2018-08-27T18:30:01.000+0000",
"UE",
"4e67d5c02b0147b1bcfc00f459c0c612"
],