Reputation: 107
I am trying to make an iPhone application that displays stock market data. I specifically want it to display the number one gainer for the day percentage wise. I have two problems: 1. Right now I only know how to display the json into the console instead of on the iphone screen via a label or something. 2. The data I get have multiple data points, when I just want the number one highest gainer.
I will now provide my code as well as the Json output I get and want on a Label:
Code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var str = "Hello, playground"
let url = URL(string: "https://financialmodelingprep.com/api/v3/stock/gainers?apikey=<api-key>")
var request = URLRequest(url: url!)
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print(error!)
return
}
guard let data = data else {
print("Data is empty")
return
}
let json = try! JSONSerialization.jsonObject(with: data, options: [])
print(json)
}
task.resume()
Json data I get, but only want the one single top percentage gainer result:
{
mostGainerStock = (
{
changes = "3.0892";
changesPercentage = "(+43.57%)";
companyName = "Cassava Sciences Inc";
price = "10.1792";
ticker = SAVA;
},
{
changes = "0.55";
changesPercentage = "(+33.95%)";
companyName = "Westport Fuel Systems Inc";
price = "2.17";
ticker = WPRT;
},
{
changes = "0.08409999999999999";
changesPercentage = "(+25.88%)";
companyName = "SandRidge Permian Trust";
price = "0.409";
ticker = PER;
},
{
changes = "0.53";
changesPercentage = "(+25.73%)";
companyName = "ReneSola Ltd";
price = "2.59";
ticker = SOL;
},
{
changes = "0.59";
changesPercentage = "(+25.00%)";
companyName = "Art's Way Manufacturing Co Inc";
price = "2.95";
ticker = ARTW;
},
{
changes = "0.06";
changesPercentage = "(+24.00%)";
companyName = "Onconova Therapeutics Inc";
price = "0.31";
ticker = ONTX;
},
{
changes = "1.33";
changesPercentage = "(+22.17%)";
companyName = "Contura Energy Inc";
price = "7.33";
ticker = CTRA;
},
{
changes = "0.48";
changesPercentage = "(+21.15%)";
companyName = "Renren Inc";
price = "2.75";
ticker = RENN;
},
{
changes = "0.2";
changesPercentage = "(+19.23%)";
companyName = "StoneMor Partners LP";
price = "1.24";
ticker = STON;
},
{
changes = "13.0999";
changesPercentage = "(+18.41%)";
companyName = "IGM Biosciences Inc";
price = "84.2399";
ticker = IGMS;
},
{
changes = "2.58";
changesPercentage = "(+17.73%)";
companyName = "Bloom Energy Corp";
price = "17.13";
ticker = BE;
},
{
changes = "0.99";
changesPercentage = "(+17.07%)";
companyName = "Flux Power Holdings Inc";
price = "6.79";
ticker = FLUX;
},
{
changes = "0.185";
changesPercentage = "(+16.97%)";
companyName = "Remark Holdings Inc";
price = "1.275";
ticker = MARK;
},
{
changes = "0.8100000000000001";
changesPercentage = "(+16.95%)";
companyName = "J Alexanders Holdings Inc";
price = "5.59";
ticker = JAX;
},
{
changes = "0.33";
changesPercentage = "(+16.92%)";
companyName = "Rexahn Pharmaceuticals Inc";
price = "2.28";
ticker = REXN;
},
{
changes = "0.23";
changesPercentage = "(+16.79%)";
companyName = "AcelRx Pharmaceuticals Inc";
price = "1.6";
ticker = ACRX;
},
{
changes = "0.79";
changesPercentage = "(+16.49%)";
companyName = "Orgenesis Inc";
price = "5.58";
ticker = ORGS;
},
{
changes = "0.405";
changesPercentage = "(+15.46%)";
companyName = "Evogene Ltd";
price = "3.025";
ticker = EVGN;
},
{
changes = "0.0426";
changesPercentage = "(+15.10%)";
companyName = "Pacific Drilling SA";
price = "0.3247";
ticker = PACD;
},
{
changes = "1.075";
changesPercentage = "(+15.05%)";
companyName = "Fluidigm Corp";
price = "8.22";
ticker = FLDM;
},
{
changes = "1.99";
changesPercentage = "(+14.85%)";
companyName = "Ituran Location and Control Ltd";
price = "15.39";
ticker = ITRN;
},
{
changes = "0.32";
changesPercentage = "(+14.41%)";
companyName = "Trevena Inc";
price = "2.54";
ticker = TRVN;
},
{
changes = "0.61";
changesPercentage = "(+14.22%)";
companyName = "Golden Bull Ltd";
price = "4.9";
ticker = DNJR;
},
{
changes = "2.005";
changesPercentage = "(+14.20%)";
companyName = "Dave & Buster's Entertainment Inc";
price = "16.125";
ticker = PLAY;
},
{
changes = "0.455";
changesPercentage = "(+14.15%)";
companyName = "Marinus Pharmaceuticals Inc";
price = "3.67";
ticker = MRNS;
},
{
changes = "1.49";
changesPercentage = "(+14.08%)";
companyName = "Pivotal Investment Corporation II";
price = "12.075";
ticker = PIC;
},
{
changes = "1.4881";
changesPercentage = "(+13.91%)";
companyName = "Eastman Kodak Co";
price = "12.1881";
ticker = KODK;
},
{
changes = "0.6";
changesPercentage = "(+13.70%)";
companyName = "NextDecade Corp";
price = "4.98";
ticker = NEXT;
},
{
changes = "0.92";
changesPercentage = "(+13.09%)";
companyName = "Trans World Entertainment Corp";
price = "7.95";
ticker = TWMC;
},
{
changes = "1.07";
changesPercentage = "(+12.85%)";
companyName = "Veritone Inc";
price = "9.4";
ticker = VERI;
}
);
}
Thanks so much for your time
Upvotes: 0
Views: 64
Reputation: 5052
There are tons of examples in SO to resolve this issue. Just in this case, I would like to use codable model to extract data.
Add this struct models to app:
struct StockModel: Codable {
let mostGainerStock: [MostGainerStock]
}
struct MostGainerStock: Codable {
let ticker: String
let changes: Double
let price, changesPercentage, companyName: String
}
After that change your code like below.
// let json = try! JSONSerialization.jsonObject(with: data, options: [])
//Add this New
if let model = try? JSONDecoder().decode(StockModel.self, from: data) {
DispatchQueue.main.async {
//Example of first data update to Label
self.tickerLbl.text = model.mostGainerStock[0].ticker
self.gainLbl.text = model.mostGainerStock[0].changesPercentage
}
}
Upvotes: 2