0

I want to parse the JSON nested array into my decodable struct but the issue is there is no Key values how can i set into my decodable struct. I am also using SwiftJSON to parse object in my project...

Here is my Decodable struct:

struct PlayerData: Decodable {
    let playerID: Int?
    let platIDRef: Int?
    let EnrolledDateTime: String?
    let PlayerType: String?
    let LastCheckIn: Double?
    let SerialNo: Int?
    let Offered: Double?
    let PlayerReferralCode: Int?
    let PlayerStats: Double?
    let PlayerUpstream: Int?

}

here is the server response JSON object in console:

you check the below json response at JSON VIEWER

[[1,1,1532890155915,0,7618.37,1,8047.76,1,0.01197066,0],[2,1,1532890155915,0,7618.37,1,8046.87,1,0.27672821,0],[3,1,1532890155915,0,7618.37,1,8045.26,1,0.27672821,0],[4,1,1532890155915,0,7618.37,1,8038.04,1,0.11222798,0],[5,1,1532890155915,0,7618.37,1,8037.94,1,0.09222818,0],[6,1,1532890155915,0,7618.37,1,8036.3,1,0.1994227,0],[7,1,1532890155915,0,7618.37,1,8035.4,1,0.44272289,0],[8,1,1532890155915,0,7618.37,1,8034.52,1,0.04333567,0],[9,1,1532890155915,0,7618.37,1,8034.51,1,0.40587824,0],[10,1,1532890155915,0,7618.37,1,8032.3,1,0.10256018,0],[11,1,1532890155915,0,7618.37,1,8031.13,1,0.00637662,0],[12,1,1532890155915,0,7618.37,1,8031.12,1,0.46122825,0],[13,1,1532890155915,0,7618.37,1,8030.34,1,0.12184043,0],[14,1,1532890155915,0,7618.37,1,8030.08,1,0.01381566,0],[15,1,1532890155915,0,7618.37,1,8028.18,1,0.01001035,0],[16,1,1532890155915,0,7618.37,1,8028.16,1,0.37072738,0],[17,1,1532890155915,0,7618.37,1,8028.14,1,0.13835319,0],[18,1,1532890155915,0,7618.37,1,8027.71,1,0.4302254,0],[19,1,1532890155915,0,7618.37,1,8026.13,1,0.002,0],[20,1,1532890155915,0,7618.37,1,8025.71,1,0.04610317,0],[21,1,1532890155915,0,7618.37,1,8379.29,1,0.00301798,1],[22,1,1532890155915,0,7618.37,1,8379.3,1,0.0698277,1],[23,1,1532890155915,0,7618.37,1,8380.3,1,0.32028728,1],[24,1,1532890155915,0,7618.37,1,8380.63,1,0.04697547,1],[25,1,1532890155915,0,7618.37,1,8382.57,1,0.01067624,1],[26,1,1532890155915,0,7618.37,1,8385.38,1,0.01161041,1],[27,1,1532890155915,0,7618.37,1,8385.4,1,0.16014364,1],[28,1,1532890155915,0,7618.37,1,8385.52,1,0.1241647,1],[29,1,1532890155915,0,7618.37,1,8386.13,1,0.12978041,1],[30,1,1532890155915,0,7618.37,1,8386.82,1,0.36803144,1],[31,1,1532890155915,0,7618.37,1,8387.9,1,0.06358541,1],[32,1,1532890155915,0,7618.37,1,8387.94,1,0.56588357,1],[33,1,1532890155915,0,7618.37,1,8389.71,1,0.10676243,1],[34,1,1532890155915,0,7618.37,1,8390.52,1,0.05338121,1],[35,1,1532890155915,0,7618.37,1,8390.61,1,0.64999101,1],[36,1,1532890155915,0,7618.37,1,8393.96,1,0.00368495,1],[37,1,1532890155915,0,7618.37,1,8393.97,1,0.32669303,1],[38,1,1532890155915,0,7618.37,1,8394.6,1,0.01363635,1],[39,1,1532890155915,0,7618.37,1,8395.11,1,0.53381214,1],[40,1,1532890155915,0,7618.37,1,8395.79,1,0.07345255,1]]
2
  • First of all there are no strings in the JSON. You have to write a custom initializer, get the unkeyedContainer() and decode the array items one by one Commented Jul 29, 2018 at 19:11
  • can you please let me know how to achieve this goal. i am very thankful Commented Jul 29, 2018 at 19:38

1 Answer 1

3

To decode an array you need to get the unkeyedContainer() and decode each value separately

let jsonString = """
[[1,1,1532890155915,0,7618.37,1,8047.76,1,0.01197066,0],[2,1,1532890155915,0,7618.37,1,8046.87,1,0.27672821,0],[3,1,1532890155915,0,7618.37,1,8045.26,1,0.27672821,0],[4,1,1532890155915,0,7618.37,1,8038.04,1,0.11222798,0],[5,1,1532890155915,0,7618.37,1,8037.94,1,0.09222818,0],[6,1,1532890155915,0,7618.37,1,8036.3,1,0.1994227,0],[7,1,1532890155915,0,7618.37,1,8035.4,1,0.44272289,0],[8,1,1532890155915,0,7618.37,1,8034.52,1,0.04333567,0],[9,1,1532890155915,0,7618.37,1,8034.51,1,0.40587824,0],[10,1,1532890155915,0,7618.37,1,8032.3,1,0.10256018,0],[11,1,1532890155915,0,7618.37,1,8031.13,1,0.00637662,0],[12,1,1532890155915,0,7618.37,1,8031.12,1,0.46122825,0],[13,1,1532890155915,0,7618.37,1,8030.34,1,0.12184043,0],[14,1,1532890155915,0,7618.37,1,8030.08,1,0.01381566,0],[15,1,1532890155915,0,7618.37,1,8028.18,1,0.01001035,0],[16,1,1532890155915,0,7618.37,1,8028.16,1,0.37072738,0],[17,1,1532890155915,0,7618.37,1,8028.14,1,0.13835319,0],[18,1,1532890155915,0,7618.37,1,8027.71,1,0.4302254,0],[19,1,1532890155915,0,7618.37,1,8026.13,1,0.002,0],[20,1,1532890155915,0,7618.37,1,8025.71,1,0.04610317,0],[21,1,1532890155915,0,7618.37,1,8379.29,1,0.00301798,1],[22,1,1532890155915,0,7618.37,1,8379.3,1,0.0698277,1],[23,1,1532890155915,0,7618.37,1,8380.3,1,0.32028728,1],[24,1,1532890155915,0,7618.37,1,8380.63,1,0.04697547,1],[25,1,1532890155915,0,7618.37,1,8382.57,1,0.01067624,1],[26,1,1532890155915,0,7618.37,1,8385.38,1,0.01161041,1],[27,1,1532890155915,0,7618.37,1,8385.4,1,0.16014364,1],[28,1,1532890155915,0,7618.37,1,8385.52,1,0.1241647,1],[29,1,1532890155915,0,7618.37,1,8386.13,1,0.12978041,1],[30,1,1532890155915,0,7618.37,1,8386.82,1,0.36803144,1],[31,1,1532890155915,0,7618.37,1,8387.9,1,0.06358541,1],[32,1,1532890155915,0,7618.37,1,8387.94,1,0.56588357,1],[33,1,1532890155915,0,7618.37,1,8389.71,1,0.10676243,1],[34,1,1532890155915,0,7618.37,1,8390.52,1,0.05338121,1],[35,1,1532890155915,0,7618.37,1,8390.61,1,0.64999101,1],[36,1,1532890155915,0,7618.37,1,8393.96,1,0.00368495,1],[37,1,1532890155915,0,7618.37,1,8393.97,1,0.32669303,1],[38,1,1532890155915,0,7618.37,1,8394.6,1,0.01363635,1],[39,1,1532890155915,0,7618.37,1,8395.11,1,0.53381214,1],[40,1,1532890155915,0,7618.37,1,8395.79,1,0.07345255,1]]
"""

struct PlayerData: Decodable {
    let playerID: Int
    let platIDRef: Int
    let enrolledDateTime: Int
    let playerType: Int
    let lastCheckIn: Double
    let serialNo: Int
    let offered: Double
    let playerReferralCode: Int
    let playerStats: Double
    let playerUpstream: Int

    init(from decoder: Decoder) throws {
        var arrayContainer = try decoder.unkeyedContainer()
        playerID = try arrayContainer.decode(Int.self)
        platIDRef = try arrayContainer.decode(Int.self)
        enrolledDateTime = try arrayContainer.decode(Int.self)
        playerType = try arrayContainer.decode(Int.self)
        lastCheckIn = try arrayContainer.decode(Double.self)
        serialNo = try arrayContainer.decode(Int.self)
        offered = try arrayContainer.decode(Double.self)
        playerReferralCode = try arrayContainer.decode(Int.self)
        playerStats = try arrayContainer.decode(Double.self)
        playerUpstream = try arrayContainer.decode(Int.self)
    }
}

The JSON can be decoded as [PlayerData].self

let data = Data(jsonString.utf8)
do {
    let result = try JSONDecoder().decode([PlayerData].self, from: data)
    print(result)
} catch { print(error) }
Sign up to request clarification or add additional context in comments.

4 Comments

I tried but getting nil here is my code let decoder = JSONDecoder() let myPlayerData = try? decoder.decode([PlayerData].self, from: data) print(marketData)
I tested the code with the given JSON in the question. Don't try? add a do - catch block and get the error
you can also check it's nested array as well please help me bro
I updated the answer with a stand-alone version. You can check it in a Playground

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.