Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ]]}

What type of decodable model will be needed for this json type to store data.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

But you can customize it as you wish, if we had more context we could help farther. For example in the list we know that there will always be two elements one represents high price and the other low price then we can do

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading