我正在尝试从Coingekco的API导入全球市值,但对于这些价值我一直都没有。我已经尝试了很多方法来使其正常工作,但总是结果为零或错误。
{"data":{"active_cryptocurrencies":4159,"upcoming_icos":78,"ongoing_icos":265,"ended_icos":2902,"markets":329,"total_market_cap":{"aed":492128933231.6131,"ars":5518864895129.366,"aud":190165694237.8515,"bch":1016474938.0969247,"bdt":11234152609784.342,"bhd":50523941951.44089,"bmd":133985552200.27586,"bnb":9110702994.781658,"brl":518269514465.8878,"btc":34246960.49967112,"cad":179689631882.41653,"chf":135057436617.8781,"clp":89944325403000.86,"cny":900583889114.154,"czk":3058782968290.539,"dkk":889865044938.1317,"eos":35688155077.1242,"eth":977954067.5311762,"eur":119260540013.46558,"gbp":102954498310.69197,"hkd":1051686095608.0157,"huf":37611084358139.58,"idr":1917339247035495.0,"ils":485751220946.8815,"inr":9379584889726.605,"jpy":14894565394973.71,"krw":151923233111717.53,"kwd":40752107658.37045,"lkr":23921780489837.266,"ltc":2300146775.8623853,"mmk":203645056814338.88,"mxn":2611746872651.9272,"myr":547684836581.4896,"nok":1171202548026.1833,"nzd":197008604359.82407,"php":7002218943538.612,"pkr":18724480919988.562,"pln":513117769983.78766,"rub":8888279967641.027,"sar":502559708470.4059,"sek":1262902259952.0542,"sgd":182100031966.49924,"thb":4251489125560.462,"try":728994621761.1099,"twd":4141091461853.9365,"usd":133985552200.27586,"vef":33293753960604988.0,"vnd":3101712671178578.0,"xag":8734106812.290735,"xau":103197012.16017447,"xdr":96238740477.7577,"xlm":1509473733327.4026,"xrp":425606410042.2238,"zar":1934738645144.5234},"total_volume":{"aed":88479324666.48027,"ars":992230705965.7842,"aud":34189682956.475758,"bch":182750921.53724423,"bdt":2019776056625.5688,"bhd":9083644470.966413,"bmd":24089116435.197468,"bnb":1638003364.1220143,"brl":93179111282.98746,"btc":6157223.711665886,"cad":32306203120.640575,"chf":24281829366.67905,"clp":16170992258027.324,"cny":161914996119.17975,"czk":549935256922.4102,"dkk":159987866804.3639,"eos":6416334514.375039,"eth":175825296.19178572,"eur":21441722538.969273,"gbp":18510077068.805733,"hkd":189081497178.9738,"huf":6762055874524.306,"idr":344716334031101.5,"ils":87332682724.16513,"inr":1686345347031.9597,"jpy":2677877682773.366,"krw":27314112540801.812,"kwd":7326777031.114606,"lkr":4300870848340.1577,"ltc":413540882.51971227,"mmk":36613122866563.16,"mxn":469563124392.19543,"myr":98467659994.28732,"nok":210569229930.3342,"nzd":35419962311.05418,"php":1258921314019.854,"pkr":3366454021818.848,"pln":92252884756.05418,"rub":1598014170431.557,"sar":90354662380.96062,"sek":227055821218.58374,"sgd":32739566325.30973,"thb":764370597327.6644,"try":131065148710.86195,"twd":744522321662.65,"usd":24089116435.197468,"vef":5985847746651143.0,"vnd":557653541427090.4,"xag":1570295546.823068,"xau":18553678.369553443,"xdr":17302658285.724358,"xlm":271386637744.6713,"xrp":76519312707.30261,"zar":347844552858.1899},"market_cap_percentage":{"btc":51.32933475999663,"eth":10.753531336738659,"xrp":9.734280880905866,"eos":2.8996722287154353,"ltc":2.6432805313548577,"bch":1.737273728052291,"bnb":1.5854271022675868,"usdt":1.5339829218051375,"etc":0.7203068369937028},"market_cap_change_percentage_24h_usd":0.9322025617759713,"updated_at":1552120049}}
struct GlobalData: Decodable {
var data: [GlobalMarketCap]
var active_cryptocurrencies: Int?
init(data: [GlobalMarketCap], active_cryptocurrencies: Int) {
self.data = data
self.active_cryptocurrencies = active_cryptocurrencies
}
}
struct GlobalMarketCap: Decodable {
var total_market_cap: [currencyMarketCap]
var markets: Int?
}
struct currencyMarketCap: Decodable{
var usd: Double?
var aud: Float?
var eur: Int?
var jpy: Double?
// init(usd: Double, aud: Float, eur: Int, jpy: Double) {
// self.usd = usd
// self.aud = aud
// self.eur = eur
// self.jpy = jpy
// }
}
func globalMarket(){
let requestURL = URL(string: "https://api.coingecko.com/api/v3/global")
let request = URLRequest(url: requestURL!)
let requestTask = URLSession.shared.dataTask(with: request) {
(data: Data?, response: URLResponse?, error: Error?) in
if(error != nil){
print("Error: \(String(describing: error))")
} else {
do {
let json = try JSONDecoder().decode(currencyMarketCap.self, from:data!)
print(json)
}catch {
print("Didn't Work")
}
}
}
requestTask.resume()
}
当我打印出json时,我总是得到:
currencyMarketCap(usd:nil,aud:nil,eur:nil,jpy:nil)
答案 0 :(得分:1)
您的数据模型与JSON数据不匹配:
struct Container: Decodable {
let data: GlobalData
}
struct GlobalData: Decodable {
var active_cryptocurrencies: Int
var total_market_cap: CurrencyMarketCap
var markets: Int
}
struct CurrencyMarketCap: Decodable {
var usd: Double?
var aud: Double?
var eur: Double?
var jpy: Double?
}
...
do {
let json = try JSONDecoder().decode(Container.self, from:data!)
print(json)
} catch {
print("Didn't Work: \(error)")
}