0

Structs を使用して nfl プレーヤーの json ファイルをデコードしようとしていますが、null ではない最初のプレーヤーの PlayerID をチェックした後も失敗し続けます。何が間違っているのかわかりませんが、ガイダンスに本当に感謝しています。これを約24時間じっと見つめていたと思います。

これは JSON ファイルのサンプルです。 ここに画像の説明を入力

ここに私の構造体があります:

// MARK: PLAYERS.JSON STRUCTS
struct playersArrayJSON: Codable {
    let lastUpdatedOn: String
    let players: Array<nflPlayers>
    
    enum CodingKeys: String, CodingKey {
        case lastUpdatedOn = "lastUpdatedOn"
        case players = "players"
    }
}

struct nflPlayers: Codable {
    let playerID: Int
    let first: String
    let last: String
    let POS: String?
    let team: nflTeam?
    let imageSrc: String?
    let currentInjury: injury?
    
    enum CodingKeys: String, CodingKey {
        case playerID = "id"
        case first = "firstName"
        case last = "lastName"
        case POS = "primaryPosition"
        case team = "currentTeam"
        case imageSrc = "officialImageSrc"
        case currentInjury = "currentInjury"
    }
}

struct nflTeam: Codable {
    let teamID: Int
    let abbv: String
    
    enum CodingKeys: String, CodingKey {
        case teamID = "id"
        case abbv = "abbreviation"
    }
}

struct injury: Codable {
    let description: String
    let designation: String
    
    enum CodingKeys: String, CodingKey {
        case description = "description"
        case designation = "playingProbability"
    }
}

Jsonをデコードするために呼び出す関数は次のとおりです。

func decodePlayersJSON() {
        // Get the players.json from docdir
        if let documentDirectory = FileManager.default.urls(for: .documentDirectory,
            in: .userDomainMask).first {
            let pathWithFileName = documentDirectory.appendingPathComponent("players.json")
            let fileUrl = URL(fileURLWithPath: pathWithFileName.path)
            do { // See if the players.json exist in docdir
                let data = try Data(contentsOf: fileUrl, options: .mappedIfSafe)
                // Exists in DocDir - Try decoding JSON and writing to jsonLocalArray
                if let searchResult: playersArrayJSON = try! JSONDecoder().decode(playersArrayJSON.self, from:data) {
                    jsonLocalArray = searchResult
                    print("Players.Json decoded successfully")
                    DispatchQueue.main.async {
                        self.lblDetails.text = "player's database updated successfully"
                        self.performSegue(withIdentifier: "toQuiz", sender: nil)
                    }
                    print(jsonLocalArray)
                } else {
                    print("Error decoding players.json.")
                }
            } catch {
                print("No data found when decoding players.json")
            }
        }
    }

コンソールに表示されるエラーは次のとおりです。

致命的なエラー: 'try!' 式が予期せずエラーを発生させました: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "players", intValue: nil), _JSONKey(stringValue) : "インデックス 0", intValue: 0)], debugDescription: "キー CodingKeys に関連付けられた値がありません (stringValue: "id", intValue: nil) ("id").", 基本エラー: nil)): ファイル CoachesChallenge/UpdatingVC .swift、293 行目 2020-10-09 18:08:08.412910-0700 CoachesChallenge[32143:2136162] 致命的なエラー:「試してください!」式が予期せずエラーを発生させました: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "players", intValue: nil),

***どんな助けやフィードバックも大歓迎です!!!! 前もって感謝します!!!

4

0 に答える 0