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

标签 arrays json swift tuples decode

我有一些数据是通过 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 {

            $0.forEach {

                switch $0 {
                case .integer(let w) :
                    print(w)
                case .integerArray(let arr) :
                    print(arr)
                }

            }
        }

    }
    catch {

        print(error)
    }

关于arrays - 在 Swift 中读取 JSON 中的嵌套数组和元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53518734/

相关文章:

c - C 中的二维字符串

java - 如何在一组数组中找到元素最多的数组

python - 当 python 出现内存错误时,如何读取文件中的有限行

ios - 更新布局 anchor 未按预期工作

ios - 关闭和其他 GCD 问题中的 Weak DispatchGroup

ios - 我怎样才能将所有数据放在一个表中,而不是拆分到多个表中?

php - undefined index 行为

php - 如何一次只获取一个键值对? PHP

json - 如何在 Go 中解析以下 JSON 结构

json - Rust 宏可以解析 JSON 吗?