Используйте Codable
для анализа вышеуказанного JSON ответа .
Codable
Модели:
Вариант-1: Если вы хотите проанализировать все данные
struct Response: Codable {
let id, currency, symbol, name: String
let logoUrl: String
let price: String
let priceDate, priceTimestamp: String
let circulatingSupply, maxSupply, marketCap, rank: String
let high: String
let highTimestamp: String
let the1D, the30D: The1_D
enum CodingKeys: String, CodingKey {
case id, currency, symbol, name, logoUrl, price, priceDate, priceTimestamp, circulatingSupply, maxSupply, marketCap, rank, high, highTimestamp
case the1D = "1d"
case the30D = "30d"
}
}
struct The1_D: Codable {
let volume, priceChange, priceChangePct, volumeChange: String
let volumeChangePct, marketCapChange, marketCapChangePct: String
}
Вариант-2: Если вам просто нужно значение price
и все игнорировать иначе,
struct Response: Codable {
let price: String
}
Анализ JSON data
вот так,
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let response = try decoder.decode([Response].self, from: data)
let price = response.first?.price //Get the value of price here..
} catch {
print(error)
}