Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.9 kB
2
Indexable
Never
import Foundation
let json =  "{\"symbol\":\"BTC\",\"name\":\"Bitcoin\",\"rank\":1,\"tags\":[{\"name\":\"Bitcoin\",\"name_vi\":\"Bitcoin\",\"_id\":\"61092d0f9dceeb15faa2ca9d\",\"slug\":\"bitcoin\",\"createdAt\":\"2021-08-03T11:48:31.935Z\",\"updatedAt\":\"2021-08-03T11:48:31.935Z\"},{\"name\":\"Top100\",\"name_vi\":\"Top100\",\"_id\":\"610926339dceeb15faa2c775\",\"slug\":\"top100\",\"createdAt\":\"2021-08-03T11:19:15.762Z\",\"updatedAt\":\"2021-08-04T17:09:14.009Z\"},{\"name\":\"ONUSPortfolio\",\"name_vi\":\"ONUSPortfolio\",\"_id\":\"610e8aede32c743eb67af917\",\"slug\":\"vndc-portfolio\",\"createdAt\":\"2021-08-07T13:30:21.611Z\",\"updatedAt\":\"2022-03-18T02:14:02.635Z\",\"index\":16},{\"name\":\"Platform\",\"name_vi\":\"Nềntảng\",\"_id\":\"610e805ae32c743eb67af3c2\",\"slug\":\"Coin\",\"createdAt\":\"2021-08-07T12:45:14.276Z\",\"updatedAt\":\"2022-03-22T04:55:28.730Z\",\"index\":13,\"imageURL\":\"https://cloudfront.goonus.io/invest/Platform.png\"}],\"category\":\"coin\",\"volume\":1729360787.9664512,\"volumeCurrency\":\"USDT\",\"volumeChangePercentage24h\":-10.817449592653883,\"statistics\":{\"price\":\"47313.77062500\",\"circulatingSupply\":18998762,\"totalSupply\":18998762,\"maxSupply\":21000000,\"marketCap\":894173775866.28,\"priceChangePercentage1h\":0.1001877644816851,\"priceChangePercentage24h\":-0.6149389719496967,\"priceChangePercentage7d\":9.948030397297192,\"priceChangePercentage30d\":9.241774791473588,\"priceChangePercentage90d\":2.0161875745533075,\"low24h\":47002.67559,\"high24h\":47856.925035,\"low52w\":28905.8175,\"high52w\":69241.5,\"lowAllTime\":2826.8595,\"highAllTime\":69241.5,\"ytdPriceChangePercentage\":-19.732408163098548,\"marketCapCurrency\":\"USDT\",\"volume\":1729360787.9664512,\"volumeCurrency\":\"USDT\",\"volumeAverage10d\":1887272722.6922767,\"volumeChangePercentage24h\":-10.817449592653883},\"viewCount\":9549462,\"view24h\":32090,\"enabled\":true,\"listingDate\":1637510700088,\"volumeAverage10d\":1887272722.6922767,\"chart\":{\"price\":\"47316.54028500\",\"chart\":[{\"bid\":\"47574.12870000\",\"time\":1648623600000},{\"bid\":\"47552.41296000\",\"time\":1648627200000},{\"bid\":\"47601.53428500\",\"time\":1648630800000},{\"bid\":\"47423.31268500\",\"time\":1648634400000},{\"bid\":\"47434.65223500\",\"time\":1648638000000},{\"bid\":\"47485.53972000\",\"time\":1648641600000},{\"bid\":\"47455.89633000\",\"time\":1648645200000},{\"bid\":\"47148.45403500\",\"time\":1648648800000},{\"bid\":\"47277.82525500\",\"time\":1648652400000},{\"bid\":\"47325.88287000\",\"time\":1648656000000},{\"bid\":\"47414.03031000\",\"time\":1648659600000},{\"bid\":\"47826.30825000\",\"time\":1648663200000},{\"bid\":\"47125.48392000\",\"time\":1648666800000},{\"bid\":\"47269.04463000\",\"time\":1648670400000},{\"bid\":\"47424.29611500\",\"time\":1648674000000},{\"bid\":\"47151.95625000\",\"time\":1648677600000},{\"bid\":\"47269.38582000\",\"time\":1648681200000},{\"bid\":\"47232.73800000\",\"time\":1648684800000},{\"bid\":\"47386.58458500\",\"time\":1648688400000},{\"bid\":\"47565.91003500\",\"time\":1648692000000},{\"bid\":\"47385.27000000\",\"time\":1648695600000},{\"bid\":\"47260.39446000\",\"time\":1648699200000},{\"bid\":\"47221.50883500\",\"time\":1648702800000},{\"bid\":\"47260.06330500\",\"time\":1648706400000},{\"bid\":\"47316.54028500\",\"time\":1648709999999}],\"change\":-0.005414464164427257}}"

func convertStringToDictionary(text: String) -> [String:AnyObject]? {
    if let data = text.data(using: .utf8) {
       do {
           let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:AnyObject]
           return json
       } catch {
           print("Something went wrong")
       }
   }
   return nil
}

let dict = convertStringToDictionary(text: json)
//print("dict \(dict!)")



struct ChartObject {
  var price: String = "" ;  // string or double
  var change: Double;
    init(price: Any , change: Double) {
        if let _price = price as? String {
            self.price = _price
        }
        if let _price = price as? Double {
            self.price = "\(_price)"
        }
        
        self.change = change
    }
}

struct ObjectData{
  var name: String = "" //fullName token
  var symbol: String = "" //code token
  var chart: ChartObject?
    
    init( dict: [String: AnyObject]) {
        
        if let name = dict["name"] as? String {
            self.name = name
        }
        
        if let symbol = dict["symbol"] as? String {
            self.symbol = symbol
        }
        
        if let price = dict["chart"]?["price"] , let change = dict["chart"]?["change"] as? Double {
            let chart = ChartObject(price: price ?? "" , change: change)
            self.chart = chart
            
        }
        
    }
}


let objData = ObjectData(dict: dict ?? [:])
print("price \(objData.chart?.price ?? "fuck")")