找不到密钥,正在努力在Swift 4中解析嵌套的JSON

时间:2019-04-27 22:05:53

标签: json swift parsing

我无法正确解析此JSON,出现以下错误:

“ keyNotFound(CodingKeys(stringValue:” GameEntries“,intValue:nil),Swift.DecodingError.Context(codingPath:[],debugDescription:”与键CodingKeys(stringValue:\“ GameEntries \”,intValue:没有关联的值: nil)(\“ GameEntries \”)。“,底层错误:nil))”

我认为我的问题可能是由于解析字典与数组而引起的,但是我开始迷路了。

JSON:

{
    fullgameschedule =     {
        gameentry =         (
                        {
                awayTeam =                 {
                    Abbreviation = SEA;
                    City = Seattle;
                    ID = 123;
                    Name = Mariners;
                };
                date = "2019-03-20";
                homeTeam =                 {
                    Abbreviation = OAK;
                    City = Oakland;
                    ID = 125;
                    Name = Athletics;
                };
                id = 48847;
                location = "Tokyo Dome";
                time = "5:35AM";
            },
                        {
                awayTeam =                 {
                    Abbreviation = CHC;
                    City = Chicago;
                    ID = 131;
                    Name = Cubs;
                };
                date = "2019-09-29";
                homeTeam =                 {
                    Abbreviation = STL;
                    City = "St. Louis";
                    ID = 133;
                    Name = Cardinals;
                };
                id = 48879;
                location = "Busch Stadium";
                time = "3:15PM";
            }
        );
        lastUpdatedOn = "2019-04-27 9:38:51 AM";
    };
}

我的结构:

struct FullGameSchedule: Decodable
{
    let GameEntries: GameEntries
    let lastUpdatedOn: String
}

struct GameEntries: Decodable
{
    let Games = [Game]()
}

struct Game: Decodable
{
    let awayTeam: Team
    let date: String
    let homeTeam: Team
    let id: Int
    let location: String
    let time: String
}

struct Team: Decodable
{
    let Abbreviation: String
    let City :String
    let ID: Int
    let Name:String
}

我的解析:

guard let url = URL(string: "blah") else { return }

        let session = URLSession.shared
        session.dataTask(with: url) { (data, response, error) in

            guard let data = data else {return }

            do {

                let games = try JSONDecoder().decode(FullGameSchedule.self, from: data)
                print(games)

                //let json = try JSONSerialization.jsonObject(with: data, options: [])
                //print(json)

            } catch {
                print(error)
            }
        }.resume()

我希望我可以将JSON放入游戏对象中,以便随后可以基于用户输入过滤并提取整个赛季中任意游戏的具体细节。

1 个答案:

答案 0 :(得分:0)

您的属性名称和前两个结构与您的JSON不匹配。您需要:

struct FullGameSchedule: Decodable
{
    let fullgameschedule: GameEntries
}

struct GameEntries: Decodable
{
    let gameentry: [Game]
    let lastUpdatedOn: String
}

您还可能希望使用指定CodingKeys的名称,以便可以使用适当的camelCase正确命名结构属性。