Need Help populating array with JSON data using SwiftyJSON


Need Help populating array with JSON data using SwiftyJSON



I want to populate an array I have with JSON data. I'm using SwiftyJSON and Alamofire. I'm new to parsing JSON, so i apologize for any ignorance on my end. The URL below is the url i'm inserting in getAllTimeAverage(). Any help would be awesome!! thanks!



This is the JSON Data
https://apiv2.bitcoinaverage.com/indices/global/history/BTCILS?period=alltime&format=json



This is the array i want to populate


var allTimeAverages: [Any] =



These are my functions. I call them in my cellForRowAt:, when i print the allTimeAverages.count, I get back zero.


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"
}
}



}





Why are you using json["average"].dictionaryObject. do you want to fetch all values of average
– Nasir
Jun 29 at 23:49


average





I want to fetch all the values. @nasir
– Adrian Navarro
Jun 29 at 23:53




1 Answer
1



Here is an example of how this is to be done (I have taken a few values from the JSON API you have shared):


let jsonString = "[{"high": 23043.41,"open": 21494.60,"low": 21338.34,"average": 21668.01,"time": "2018-06-29 00:00:00","volume": 102148.30132998565},{"high": 22488.75,"open": 22405.51,"low": 21380.97,"average": 22241.29,"time": "2018-06-28 00:00:00","volume": 69383.44795111718},{"high": 22491.85,"open": 22169.36,"low": 21940.47,"average": 22224.29,"time": "2018-06-27 00:00:00","volume": 69884.07809550199},{"high": 22707.46,"open": 22635.32,"low": 22004.29,"average": 22480.86,"time": "2018-06-26 00:00:00","volume": 71611.8914173987}]";

if let jsonData = jsonString.data(using: .utf8, allowLossyConversion: false) {
let json = JSON(data: jsonData)
let jsonArray = json.arrayValue

let averagesArray = jsonArray.map{ $0["average"].doubleValue}
let highArray = jsonArray.map{ $0["high"].doubleValue}
let lowArray = jsonArray.map{ $0["low"].doubleValue}
let volumeArray = jsonArray.map{ $0["volume"].doubleValue}
let openArray = jsonArray.map{ $0["open"].doubleValue}

}



Making your solution work according to the above:
In the following line, remove as you are already getting an array from the API.


let bitcoinJSON : JSON = [JSON(response.result.value!)]



so the above line will become


let bitcoinJSON : JSON = JSON(response.result.value!)



Now, you can get the array value like I have done:


let jsonArray = bitcoinJSON.arrayValue



and now you can utilize the rest of the code (repeated below):


let averagesArray = jsonArray.map{ $0["average"].doubleValue}
let highArray = jsonArray.map{ $0["high"].doubleValue}
let lowArray = jsonArray.map{ $0["low"].doubleValue}
let volumeArray = jsonArray.map{ $0["volume"].doubleValue}
let openArray = jsonArray.map{ $0["open"].doubleValue}





getting this error : Value of tuple type '(String, JSON)' has no member 'subscript'
– Adrian Navarro
Jun 30 at 0:03





I have edited the answer
– Nasir
Jun 30 at 0:06





i feel like i'm putting the constant is the wrong place. I'm not getting anything
– Adrian Navarro
Jun 30 at 0:14





Check my edited answer
– Nasir
2 days ago





Can you please upvote the answer (and/or select the answer) if it worked for you
– Nasir
yesterday






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV