Last active
December 10, 2017 18:56
-
-
Save z4r/fb3549083c50f5b92818993141a8defe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| let jsonData = try! Data(contentsOf: Bundle.main.url(forResource: "stats", withExtension: "json")!) | |
| let teamStats = try! JSONDecoder().decode(TeamStats.self, from: jsonData).elements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "overallteamstandings": { | |
| "lastUpdatedOn": "2017-12-10 2:29:19 AM", | |
| "teamstandingsentry": [{ | |
| "team": { | |
| "ID": "109", | |
| "City": "Houston", | |
| "Name": "Rockets", | |
| "Abbreviation": "HOU" | |
| }, | |
| "rank": "1", | |
| "stats": { | |
| "GamesPlayed": { | |
| "@abbreviation": "G", | |
| "#text": "24" | |
| }, | |
| "Wins": { | |
| "@category": "Standings", | |
| "@abbreviation": "W", | |
| "#text": "20" | |
| }, | |
| "Losses": { | |
| "@category": "Standings", | |
| "@abbreviation": "L", | |
| "#text": "4" | |
| } | |
| } | |
| }, | |
| { | |
| "team": { | |
| "ID": "82", | |
| "City": "Boston", | |
| "Name": "Celtics", | |
| "Abbreviation": "BOS" | |
| }, | |
| "rank": "2", | |
| "stats": { | |
| "GamesPlayed": { | |
| "@abbreviation": "G", | |
| "#text": "27" | |
| }, | |
| "Wins": { | |
| "@category": "Standings", | |
| "@abbreviation": "W", | |
| "#text": "22" | |
| }, | |
| "Losses": { | |
| "@category": "Standings", | |
| "@abbreviation": "L", | |
| "#text": "5" | |
| } | |
| } | |
| }, | |
| { | |
| "team": { | |
| "ID": "101", | |
| "City": "Golden State", | |
| "Name": "Warriors", | |
| "Abbreviation": "GSW" | |
| }, | |
| "rank": "3", | |
| "stats": { | |
| "GamesPlayed": { | |
| "@abbreviation": "G", | |
| "#text": "27" | |
| }, | |
| "Wins": { | |
| "@category": "Standings", | |
| "@abbreviation": "W", | |
| "#text": "21" | |
| }, | |
| "Losses": { | |
| "@category": "Standings", | |
| "@abbreviation": "L", | |
| "#text": "6" | |
| } | |
| } | |
| } | |
| ] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| struct TeamStats { | |
| let elements: [TeamStat] | |
| } | |
| extension TeamStats: Decodable { | |
| enum CodingKeys: String, CodingKey { | |
| case overallteamstandings | |
| } | |
| enum OATSKeys: String, CodingKey { | |
| case teamstandingsentry | |
| } | |
| public init(from decoder: Decoder) throws { | |
| let values = try decoder.container(keyedBy: CodingKeys.self) | |
| let oats = try values.nestedContainer(keyedBy: OATSKeys.self, forKey: .overallteamstandings) | |
| self.elements = try oats.decode([TeamStat].self, forKey: .teamstandingsentry) | |
| } | |
| } | |
| struct TeamStat { | |
| let city: String | |
| let name: String | |
| let wins: String | |
| let losses: String | |
| } | |
| extension TeamStat: Decodable { | |
| enum CodingKeys: String, CodingKey { | |
| case team, stats | |
| } | |
| enum TeamKeys: String, CodingKey { | |
| case city = "City" | |
| case name = "Name" | |
| } | |
| enum StatsKeys: String, CodingKey { | |
| case wins = "Wins" | |
| case losses = "Losses" | |
| } | |
| enum WinsLossesKeys: String, CodingKey { | |
| case text = "#text" | |
| } | |
| public init(from decoder: Decoder) throws { | |
| let values = try decoder.container(keyedBy: CodingKeys.self) | |
| let team = try values.nestedContainer(keyedBy: TeamKeys.self, forKey: .team) | |
| self.city = try team.decode(String.self, forKey: .city) | |
| self.name = try team.decode(String.self, forKey: .name) | |
| let stats = try values.nestedContainer(keyedBy: StatsKeys.self, forKey: .stats) | |
| let wins = try stats.nestedContainer(keyedBy: WinsLossesKeys.self, forKey: .wins) | |
| self.wins = try wins.decode(String.self, forKey: .text) | |
| let losses = try stats.nestedContainer(keyedBy: WinsLossesKeys.self, forKey: .losses) | |
| self.losses = try losses.decode(String.self, forKey: .text) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment