Hi I am trying to load the JSON and parsing it into another array which just contains the dictionaries of the teams. I am successful to load every team name into the array but I can't quite wrap my head around how to get the dictionaries containing ale the info in the array.
Link to the JSON: https://statsapi.web.nhl.com/api/v1/standings
This is what I want for each team in the array
team: {
id: 15,
name: "Washington Capitals",
link: "/api/v1/teams/15"
},
leagueRecord: {
wins: 49,
losses: 26,
ot: 7,
type: "league"
},
goalsAgainst: 239,
goalsScored: 259,
points: 105,
divisionRank: "1",
conferenceRank: "3",
leagueRank: "6",
wildCardRank: "0",
row: 46,
gamesPlayed: 82,
streak: {
streakType: "wins",
streakNumber: 1,
streakCode: "W1"
},
clinchIndicator: "y",
lastUpdated: "2018-05-08T00:46:01Z"
}
this is the class where I load and parse the JSON
import UIKit
class StandingsTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var standingsURL: URL = URL(string: "https://statsapi.web.nhl.com/api/v1/standings")!
@IBOutlet weak var tableView: UITableView!
var standingData: [Records] = []
var standings = [String]()
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
override func viewDidLoad() {
super.viewDidLoad()
loadStandings()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func loadStandings(){
print("load standings")
view.addSubview(activityIndicator)
activityIndicator.frame = view.bounds
activityIndicator.startAnimating()
let standingsDatatask = URLSession.shared.dataTask(with: standingsURL, completionHandler: dataLoaded)
standingsDatatask.resume()
}
func dataLoaded(data:Data?,response:URLResponse?,error:Error?){
if let standingsDetailData = data{
let decoder = JSONDecoder()
do {
let jsondata = try decoder.decode(Initial.self, from: standingsDetailData)
var i = 0
var j = 0
standingData = jsondata.records!
for _ in standingData{
for _ in standingData[i].teamRecords {
standings.append(standingData[i].teamRecords[j].team.name)
j+=1
if(j >= standingData[i].teamRecords.count) {
j = 0
}
}
i+=1
}
print(standings[0])
DispatchQueue.main.async{
self.tableView.reloadData()
self.activityIndicator.removeFromSuperview()
}
}catch let error{
print(error)
}
}else{
print(error!)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return standings.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "standingsCell", for: indexPath) as! standingsCell
cell.teamImage.image = UIImage(named: standings[indexPath.row])
// cell.gamesPlayed.text = String(standingData[indexPath.row].teamRecords[indexPath.row].gamesPlayed)
// cell.wins.text = String(standingData[indexPath.row].teamRecords[indexPath.row].leagueRecord.wins)
// cell.loses.text = String(standingData[indexPath.row].teamRecords[indexPath.row].leagueRecord.losses)
// cell.overTime.text = String(standingData[indexPath.row].teamRecords[indexPath.row].leagueRecord.ot!)
// cell.points.text = String(standingData[indexPath.row].teamRecords[indexPath.row].points)
return cell
}
}
and this is my dataModel
import Foundation
//Schedule
struct Initial: Codable {
let totalGames: Int?
let dates: [Dates]?
let teams: [Teams]?
let records: [Records]?
}
struct Dates: Codable {
let date: String?
let totalGames: Int
let games: [Game]
}
struct Game: Codable {
let link: String
let gameDate: String
let status: Status
let teams: Team
let venue: Venue
let content: Content
}
struct Status: Codable {
let abstractGameState: String
let codedGameState: String
let detailedState: String
let statusCode: String
let startTimeTBD: Bool
}
struct Team: Codable {
let away: Away
let home: Home
}
struct Away: Codable {
let team: TeamInfo
}
struct Home: Codable {
let team: TeamInfo
}
struct LeagueRecord: Codable {
let wins: Int
let losses: Int
let type: String
let ot: Int?
}
struct TeamInfo: Codable {
let id: Int
let name: String
let link: String
}
struct Venue: Codable {
let name: String
let link: String
}
struct Content: Codable {
let link: String
}
//teams
struct Teams: Codable {
let id: Int
let name: String
let link: String
let abbreviation: String
let teamName: String
let locationName: String
let firstYearOfPlay: String?
let officialSiteUrl: String
let franchiseId: Int
let venue: Venue
let division: Division
let conference: Conference
let franchise: Franchise
}
struct Division: Codable {
let id: Int
let name: String
let link: String
}
struct Conference: Codable {
let id: Int
let name: String
let link: String
}
struct Franchise: Codable {
let franchiseId: Int
let link: String
}
//standings
struct Records: Codable {
let division: Division
let conference: Conference
let teamRecords: [TeamRecords]
}
struct TeamRecords: Codable {
let team: TeamInfo
let leagueRecord: LeagueRecord
let points: Int
let gamesPlayed: Int
}
struct Standings: Codable {
let teamRecords: [TeamRecords]
}