从json中获取单个元素

时间:2019-07-31 08:13:39

标签: ios json swift

我有一个像这样的json响应:

{
    "Matches":[
        {
            "id": 1,
            "Teams": [
                {"id": 1, "score": 254},
                {"id": 2, "score": 253}
            ]
        },
        {
            "id": 2,
            "Teams": [
                {"id": 1, "score": 330},
                {"id": 3, "score": 331}
            ]
        },
        {
            "id": 3,
            "Teams": [
                {"id": 1, "score": 220},
                {"id": 4, "score": 220}
            ]
        },
        {
            "id": 4,
            "Teams": [
                {"id": 1, "score": 264},
                {"id": 5, "score": 189}
            ]
        },
        {
            "id": 5,
            "Teams": [
                {"id": 2, "score": 240},
                {"id": 3, "score": 200}
            ]
        },
        {
            "id": 6,
            "Teams": [
                {"id": 2, "score": 330},
                {"id": 4, "score": 331}
            ]
        },
        {
            "id": 7,
            "Teams": [
                {"id": 2, "score": 320},
                {"id": 5, "score": 220}
            ]
        },
        {
            "id": 8,
            "Teams": [
                {"id": 3, "score": 320},
                {"id": 4, "score": 300}
            ]
        },
        {
            "id": 9,
            "Teams": [
                {"id": 3, "score": 280},
                {"id": 5, "score": 300}
            ]
        },
        {
            "id": 10,
            "Teams": [
                {"id": 4, "score": 180},
                {"id": 5, "score": 180}
            ]
        }
    ]
}

这是我为上述响应提供的结构:

struct Matches: Codable {
    let matches: [Match]

    enum CodingKeys: String, CodingKey {
        case matches = "Matches"
    }
}

// MARK: - Match
struct Match: Codable {
    let id: Int
    let teams: [Team]

    enum CodingKeys: String, CodingKey {
        case id
        case teams = "Teams"
    }
}

// MARK: - Team
struct Team: Codable {
    let id, score: Int
}

这就是我进行api调用以获取数据的方式:

fileprivate func getMatches() {
    URLSession.shared.dataTask(with: URL(string: "URL HERE")!) { (data, response, error) in
        guard let data = data else { return }
        do {
            let res = try JSONDecoder().decode(Matches.self, from: data)

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

但是我无法弄清楚如何比较Teams响应中json中2个id的得分,并说出哪个ID更大以及每个ID的计数ID的..

1 个答案:

答案 0 :(得分:4)

第1队比赛

let team1 = res.matches.filter {$0.teams.contains { $0.id == 1 }}

找到每场比赛的获胜者,请使用以下扩展名:

extension Match {
    var winnerTeam: Team {
        return teams.sorted { $0.score > $1.score }.first!
    }
}

例如,获奖者名单如下:

let winners = res.matches.map { $0.winnerTeam }

类似地,寻找更宽松的团队:

extension Match {
    var looserTeam: Team {
        return teams.sorted { $0.score > $1.score }.last!
    }
}

用于按ID计数获胜团队:

func scoresOfWinners() -> [Int: Int] {
    var scores = [Int: Int]()
    for team in winners {
        scores[team.id] = (scores[team.id] ?? 0) + 1
    }
    return scores
}

它将返回ID列表:获胜计数。

请记住,如果您想处理tie的情况,则在选择获胜者或宽松者之前应谨慎行事:

var winnerTeam: Team? {
    guard teams.first!.score != teams.last!.score else { return nil }
    return teams.sorted { $0.score > $1.score }.first!
}