Hello I have been trying to decode a JSON file into my swift project but I am receiving this error. Any help would be much appreciated.
dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 2", intValue: 2), CodingKeys(stringValue: "odds", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "total", intValue: nil), CodingKeys(stringValue: "current", intValue: nil), CodingKeys(stringValue: "total", intValue: nil)], debugDescription: "Parsed JSON number <212.5> does not fit in Int.", underlyingError: nil))
Here is my code and the JSON:
struct Games: Decodable {
var status: Int?
var time: String?
var games: Int?
var skip: Int?
var results: [GameResult]?
}
struct GameResult: Decodable {
var schedule: GameSchedule?
var summary: String?
var deatails: GameDetails?
var status: String?
var teams: TeamInfo?
var lastUpdated: String?
var gameId: Int?
var venue: GameVenue?
var odds: [GameOdds]?
}
struct GameSchedule: Decodable {
var date: String?
var tbaTime: Bool?
}
struct GameDetails: Decodable {
var league: String?
var seasonType: String?
var season: Int?
var conferenceGame: Bool?
var divisionGame: Bool?
}
struct GameVenue: Decodable {
var name: String?
var city: String?
var state: String?
var neutralSite: Bool?
}
struct GameOdds: Decodable {
var spread: GameSpread?
var moneyLine: GameMoneyLine?
var total: GameTotal?
var openDate: String?
var lastUpdated: String?
}
struct GameSpread: Decodable {
var open: OpenSpread?
var current: CurrentSpread?
}
struct OpenSpread: Decodable {
var away: Double?
var home: Double?
var awayOdds: Int?
var homeOdds: Int?
}
struct CurrentSpread: Decodable {
var away: Double?
var home: Double?
var awayOdds: Int?
var homeOdds: Int?
}
struct GameMoneyLine: Decodable {
var open: OpenMoney?
var current: CurrentMoney?
}
struct OpenMoney: Decodable {
var awayOdds: Int?
var homeOdds: Int?
}
struct CurrentMoney: Decodable {
var awayOdds: Int?
var homeOdds: Int?
}
struct GameTotal: Decodable {
var open: OpenTotal?
var current: CurrentTotal?
}
struct OpenTotal: Decodable {
var total: Int?
var overOdds: Int?
var underOdds: Int?
}
struct CurrentTotal: Decodable {
var total: Int?
var overOdds: Int?
var underOdds: Int?
}
struct TeamInfo: Decodable {
var away: AwayTeams?
var home: HomeTeams?
}
struct AwayTeams: Decodable {
var team: String?
var location: String?
var mascot: String?
var abbreviation: String?
var conference: String?
var division: String?
}
struct HomeTeams: Decodable {
var team: String?
var location: String?
var mascot: String?
var abbreviation: String?
var conference: String?
var division: String?
}
``` if error == nil {
do {
let decoder = JSONDecoder()
let result = try decoder.decode(Games.self, from: data!)
print(result)
}
catch {
print(error)
}
}
Problem is with "Index 2" nor "Index 0".
Seems to be results and odds.
Probably the Games struct (with all sub struct) does not match the JSON. Did you check with care ?
There seems to be at least a typo on deatails
in struct GameResult:
var deatails: GameDetails?
vs
var details: GameDetails?
There may well be a second error like this first.
You name a var open, which is Swift reserved in
var open: OpenSpread?
That may cause problem.
To solve it, define Coding keys, with a different name for the var and "open" for the codingKey.