在 Swift 中读取 JSON 中的嵌套数组和元组

Read nested array and tuples in JSON in Swift

我有一些数据通过 JSON 传递,这是一个包含数组的元组数组,我无法从中提取数据。我尝试转换为不同的类型,但唯一有效的是 [Any] 并且我无法进一步分解它。 JSON:

{
  ...
  // stuff that I can easily read, like "WIDTH": 33,
  ... 
  "WIDTHS_ROI_PAIRS": [[80, [0,0,200,160]], [145, [0, 240, 100, 60]], [145, [100, 240, 100, 60]]]
}

它要进入的结构:

struct WidthRoiPair: Codable {
    let width: Int
    let roi: [Int]
}

我想做的(不行,当作伪代码):

let widthRoiPairsTmp = json["WIDTHS_ROI_PAIRS"] as! [Any]
for p in widthRoiPairsTmp {
    let pair = WidthRoiPair(width: p.0 as! Int, roi:p.1 as! [Int])
    widthRoiPairs.append(pair)
}

尝试使用 p[0] 而不是 p.0 也不起作用,尝试将 JSON 直接转换为我需要的内容,如下所示:

let widthRoiPairsTmp = json["WIDTHS_ROI_PAIRS"] as! [(Int, [Int])]

也不行。我尝试使用 JSONDecoder() 但我不知道如何将 json["WIDTHS_ROI_PAIRS"] (或其元素)传递给它(如何将其转换回数据)。 我敢肯定,对于任何对 Swift 有更多经验的人来说,答案都是显而易见的,但此刻我完全被困住了...

你可以试试

struct Root: Codable {
    let widthsRoiPairs: [[InnerItem]]

    enum CodingKeys: String, CodingKey {
        case widthsRoiPairs = "WIDTHS_ROI_PAIRS"
    }
}

enum InnerItem: Codable {
    case integer(Int)
    case integerArray([Int])

    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([Int].self) {
            self = .integerArray(x)
            return
        }
        throw DecodingError.typeMismatch(InnerItem.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for InnerItem"))
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .integer(let x):
            try container.encode(x)
        case .integerArray(let x):
            try container.encode(x)
        }
    }
}

用法

let str = """

            {
            "WIDTHS_ROI_PAIRS": [[80, [0,0,200,160]], [145, [0, 240, 100, 60]], [145, [100, 240, 100, 60]]]
            }

"""

    do {

        let res = try JSONDecoder().decode(Root.self, from: str.data(using: .utf8)!)

        res.widthsRoiPairs.forEach {

            [=11=].forEach {

                switch [=11=] {
                case .integer(let w) :
                    print(w)
                case .integerArray(let arr) :
                    print(arr)
                }

            }
        }

    }
    catch {

        print(error)
    }