【发布时间】:2018-06-29 22:02:23
【问题描述】:
我想用 JSON 数据填充我拥有的数组。我正在使用 SwiftyJSON 和 Alamofire。我是解析 JSON 的新手,所以对于我的任何无知,我深表歉意。下面的 URL 是我在 getAllTimeAverage() 中插入的 url。任何帮助都是极好的!!谢谢!
这是 JSON 数据 https://apiv2.bitcoinaverage.com/indices/global/history/BTCILS?period=alltime&format=json
这是我要填充的数组
var allTimeAverages: [Any] = []
这些是我的功能。我在我的 cellForRowAt: 中调用它们,当我打印 allTimeAverages.count 时,我得到了零。
func getAllTimeAverage(url: String) {
Alamofire.request(url, method: .get)
.responseJSON { response in
if response.result.isSuccess {
print("Sucess! Got the Bitcoin Data")
let bitcointJSON : JSON = [JSON(response.result.value!)]
self.updateAverageAllTime(json: bitcointJSON)
} else {
print("Error: \(String(describing: response.result.error))")
self.price = "Connection Issues"
}
}
}
func updateAverageAllTime(json: JSON) {
if let allTimeAverage = json["average"].dictionaryObject {
self.allTimeAverages.append(allTimeAverage)
}
}
func getBitcoinData(url: String) {
Alamofire.request(url, method: .get)
.responseJSON { response in
if response.result.isSuccess {
print("Sucess! Got the Bitcoin Data")
let bitcointJSON : JSON = JSON(response.result.value!)
self.updateBitcoinData(json: bitcointJSON)
} else {
print("Error: \(String(describing: response.result.error))")
self.price = "Connection Issues"
}
}
}
【问题讨论】:
-
你为什么使用 json["average"].dictionaryObject.你想获取
average的所有值吗? -
我想获取所有的值。 @nasir
标签: arrays swift swifty-json