- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 Coingekco 的 API 导入全局市值,但我一直没有得到值。我已经尝试了很多方法来让它工作,但总是出现 nil 或错误。
{"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)
最佳答案
您的数据模型与 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)")
}
关于ios - Swift Json 值不断上升为 "nil",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075982/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!