繁体   English   中英

使用 codable 解码 JSON 数组 - Swift 5

[英]Decoding JSON array with codable - Swift 5

我想解码以下链接中的json数据

https://stats.nba.com/stats/assistleaders?LeagueID=00&PerMode=Totals&PlayerOrTeam=Team&Season=2019-20&SeasonType=Regular+Season

但是,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]

}

困难的部分是处理应该包含多种数据类型的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)
        }
    }
}

这种 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)
    }
    
}

正如评论中提到的,请遵守命名约定:结构、类和枚举以大写字母开头,变量、属性、函数和枚举案例以小写字母开头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM