1

so I'm trying to request an api with json decoder using a decodable struct, the problem is that one of the json's property returned starts with a number like "24h_change" : "12323", the question is how to convert that property into a variable, here's my code:

struct CoinModel : Decodable {

let 24h_volume_usd : String? // here's the problem
let id : String?
let name : String?
let symbol :String?
let rank: String?
let price_usd: String?
}

the api returns

[{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "8660.0",
"24h_volume_usd": "1231213"
}]

ViewController

  URLSession.shared.dataTask(with: apiUrl) { (data, Reponse, err) in

        if err != nil{
            print("error getting JSON")
        }
        guard let data = data else{return}

        do{
            let JSON = try JSONDecoder().decode([CoinModel].self, from: data)

            self.coinArray = JSON


        }catch let err {

            print (err)
        }

     }.resume()
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Bruno Henrique
  • 131
  • 4
  • 11

0 Answers0