如何将此 .json 解码为 swift 模型,因为此 json 没有密钥?

How to decode this .json to swift model as this json does not have keys?

{
  "prices": [
    [
      1649057685537,
      46171.36635962779
    ],
    [
      1649057980881,
      46145.23327887388
    ],
    [
      1649058304446,
      46182.34647884338
    ]
  ]
}

这种json类型需要什么类型的可解码模型来存储数据。

struct Prices: Codable {
  let prices: [[Double]]
}

但是您可以根据需要自定义它,如果我们有更多的上下文,我们可以提供更多帮助。例如在列表中,我们知道总会有两个元素,一个代表高价,另一个代表低价,那么我们可以做

struct Price {
  let high: Double
  let low: Double
  init(list: [Double] {
    self.high = list[0]
    self.low = list[1]
  }
}
struct Prices: Codable {
  let prices: [Price]
  enum CodingKeys: String, CodingKey {
    case prices
  }
  init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.prices = try container.decode([[Double]].self, forKey: .prices).map(Price.init)
  }
   … todo ecode if you really want to conform to Codable. But just parsing can be just Decodable
}

我知道如何设计模型了;

struct ChartPoints: Codable {
    let prices: [[Double]]
}

这样设计模型并没有报错

我会创建一个自定义类型来保存价格和价格日期,其中子数组在自定义 init(from:)

中转换为正确的值
struct Root: Codable {
    let prices: [PriceInfo]
}

struct PriceInfo: Codable {
    let price: Double
    let time: Date

    init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let value = try container.decode(Int.self)
        time = Date(timeIntervalSince1970: TimeInterval(value) / 1000)
        price = try container.decode(Double.self)
    }
}