'Deserialize double array

I try to deserialize some JSON on Swift 4.2 with Codable Protocol. My Json:

{  
   "status":1,
   "data":[  
      [  
         {  
            "id":"4klJeiCKTs",
            "body":"first",
            "da":"1442236233",
            "dm":"1442236233"
         },
         {  
            ...
         }
      ]
   ]
}

my structures and code:

struct GetEntriesRequest: Decodable{
    var status: Int
    var data: [NestedArrayGetEntries]
}

struct NestedArrayGetEntries: Decodable{
    var elements: [GetEntriesDataFromSession]
}
struct GetEntriesDataFromSession: Decodable{
    var id: String
    var body: String
    var da: String
    var dm: String
}
...
let decoder = JSONDecoder()
            
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            do {
                let root = try decoder.decode(GetEntriesRequest.self, from: data)
                dataSession = root
            } catch { print(error) }

Also, I tried this struct

var data: [[GetEntriesDataFromSession]]

but without any success.



Solution 1:[1]

data key is a nested array and there is no elements key

 var data: [NestedArrayGetEntries]

with

var data: [[NestedArrayGetEntries]]

struct GetEntriesRequest: Codable {
    let status: Int
    let data: [[NestedArrayGetEntries]]
}

struct NestedArrayGetEntries: Codable {
    let id, body, da, dm: String
}

You don't also need a snakeCase here

  do {
      let root = try JSONDecoder().decode(GetEntriesRequest.self, from: data)
      dataSession = root
   } catch { 
      print(error)
   }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1