我想在下面的链接中解码json数据
但是,rowSet数据是数组中一组json数组。如何设置可编码结构来解码此数据?我可以设置以下结构来解码其他数据。
import Foundation
struct leagueLeader: Codable {
var resource: String
var parameters: parameters
}
struct parameters: Codable {
var LeagueID: String
var PerMode: String
var StatCategory: String
var Season: String
var SeasonType: String
}
struct resultSet: Codable {
var name: String
var headers: [String]
}发布于 2020-10-12 12:18:30
困难的部分是处理应该包含多种数据类型的rowSet
{
"rowSet": [
[
1,
1610612756,
"PHX",
"Phoenix Suns",
1987
]
]
}解决方案是声明一个枚举,每个case都有一个表示可编码数据类型的关联值
enum RowSet: Codable, Equatable {
case integer(Int)
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Int.self) {
self = .integer(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(RowSet.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for RowSet"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}充满了模型:
import Foundation
struct Leader: Codable, Equatable {
let resource: String?
let parameters: Parameters?
let resultSets: [ResultSet]?
}
struct Parameters: Codable, Equatable {
let leagueId, season, seasonType, perMode: String?
let playerOrTeam: String?
enum CodingKeys: String, CodingKey {
case leagueId = "LeagueID"
case season = "Season"
case seasonType = "SeasonType"
case perMode = "PerMode"
case playerOrTeam = "PlayerOrTeam"
}
}
struct ResultSet: Codable, Equatable {
let name: String?
let headers: [String]?
let rowSet: [[RowSet]]?
}
enum RowSet: Codable, Equatable {
case integer(Int)
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Int.self) {
self = .integer(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(RowSet.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for RowSet"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}发布于 2020-10-12 12:56:58
这种JSON结构非常不寻常。它是一种逗号分隔值(CSV),不能隐式解码为结构。
一种解决方案是手动解码数组
struct RowSet: Decodable {
let rank, teamID : Int
let teamAbbreviation, teamName : String
let ast : Int
init(from decoder: Decoder) throws {
var arrayContainer = try decoder.unkeyedContainer()
guard arrayContainer.count == 5 else { throw DecodingError.dataCorruptedError(in: arrayContainer, debugDescription: "The array must contain 5 items") }
rank = try arrayContainer.decode(Int.self)
teamID = try arrayContainer.decode(Int.self)
teamAbbreviation = try arrayContainer.decode(String.self)
teamName = try arrayContainer.decode(String.self)
ast = try arrayContainer.decode(Int.self)
}
}如注释中所述,请遵循命名约定:结构、类和枚举以大写字母开头,变量、属性、函数和枚举大小写以小写字母开头。
https://stackoverflow.com/questions/64311232
复制相似问题