我想将 JSON 嵌套数组解析为我的可分解结构,但是问题是没有键值,我该如何设置到我的可分解的结构< / strong>。我还使用SwiftJSON来解析项目中的对象...
这是我的可分解结构:
(end:-4:1)
这是控制台中的服务器响应JSON对象:
您在JSON VIEWER处查看以下json响应
hello-world
答案 0 :(得分:1)
要解码数组,您需要获取unkeyedContainer()
并分别解码每个值
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)
}
}
JSON可以解码为[PlayerData].self
let data = Data(jsonString.utf8)
do {
let result = try JSONDecoder().decode([PlayerData].self, from: data)
print(result)
} catch { print(error) }