簡體   English   中英

使用可選值解碼嵌套JSON Swift 4

[英]Decoding Nested JSON with Optional Values Swift 4

我正在嘗試從此URL https://jsonodds.com/api/test/odds解碼此JSON

有時其中一些值為nil,我想避免將所有屬性都設為可選,所以我嘗試實現自定義解碼。

[
    {
        "ID": "e83bf033-dbde-49ac-a073-d385ebdc66d1",
        "HomeTeam": "Xavier",
        "AwayTeam": "Marquette",
        "Sport": 2,
        "MatchTime": "2018-01-24T23:30:00",
        "Odds": [
            {
                "ID": "50742eed-a1c8-4958-8678-50627ffb665e",
                "EventID": "e83bf033-dbde-49ac-a073-d385ebdc66d1",
                "MoneyLineHome": "0",
                "MoneyLineAway": "0",
                "PointSpreadHome": "-4.0",
                "PointSpreadAway": "4.0",
                "PointSpreadHomeLine": "-110",
                "PointSpreadAwayLine": "-110",
                "TotalNumber": "78.0",
                "OverLine": "-115",
                "UnderLine": "-105",
                "DrawLine": "0",
                "LastUpdated": "2018-01-24T20:50:17",
                "SiteID": 3,
                "OddType": "First Half"
            },
            {
                "ID": "e83bf033-dbde-49ac-a073-d385ebdc66d1",
                "EventID": "e83bf033-dbde-49ac-a073-d385ebdc66d1",
                "MoneyLineHome": "-305",
                "MoneyLineAway": "255",
                "PointSpreadHome": "-7.0",
                "PointSpreadAway": "7.0",
                "PointSpreadHomeLine": "-112",
                "PointSpreadAwayLine": "-108",
                "TotalNumber": "162.5",
                "OverLine": "-104",
                "UnderLine": "-116",
                "DrawLine": "0",
                "LastUpdated": "2018-01-24T20:50:17",
                "SiteID": 3,
                "OddType": "Game"
            }
        ],
        "Details": "Marquette at Xavier",
        "HomeROT": "720",
        "AwayROT": "719"
    },

我目前有這個,但是我不知道我是否在計算值何時為零,並且由於JSON中的“ Odds”鍵是一個數組,因此也很難使它起作用,所以我不確定該怎么做關於這樣做。 當JSON中的“ OddType”等於“ Game”時,我僅需要“ Odds”。 從JSON中獲取所有數據后,我只是將其過濾掉,因為我不確定在解析JSON時是否有辦法做到這一點。

struct GameInformation: Codable {    
    let homeTeam: String
    let awayTeam: String
    let sport: Int
    let matchTime: String
    let odds: [Odds]
    let details: String
}

struct Odds: Codable {
    let moneyLineHome: String
    let moneyLineAway: String
    let pointSpreadHome: String
    let pointSpreadAway: String
    let pointSpreadHomeLine: String
    let pointSpreadAwayLine: String
    let totalNumber: String
    let overLine: String
    let underLine: String
    let drawLine: String
    let lastUpdated: String
    let oddType: String
}

extension Odds {
    enum CodingKeys: String, CodingKey {
        case moneyLineHome = "MoneyLineHome"
        case moneyLineAway = "MoneyLineAway"
        case pointSpreadHome = "PointSpreadHome"
        case pointSpreadAway = "PointSpreadAway"
        case pointSpreadHomeLine = "PointSpreadHomeLine"
        case pointSpreadAwayLine = "PointSpreadAwayLine"
        case totalNumber = "TotalNumber"
        case overLine = "OverLine"
        case underLine = "UnderLine"
        case drawLine = "DrawLine"
        case lastUpdated = "LastUpdated"
        case oddType = "OddType"
    }

    enum OddsKey: String, CodingKey {
        case odds = "Odds"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: OddsKey.self)
        let oddsContainer = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .odds)

        moneyLineHome = try oddsContainer.decodeIfPresent(String.self, forKey: .moneyLineHome) ?? "N/A"
        moneyLineAway = try oddsContainer.decodeIfPresent(String.self, forKey: .moneyLineAway) ?? "N/A"
        pointSpreadHome = try oddsContainer.decodeIfPresent(String.self, forKey: .pointSpreadHome) ?? "N/A"
        pointSpreadAway = try oddsContainer.decodeIfPresent(String.self, forKey: .pointSpreadAway) ?? "N/A"
        pointSpreadHomeLine = try oddsContainer.decodeIfPresent(String.self, forKey: .pointSpreadHomeLine) ?? "N/A"
        pointSpreadAwayLine = try oddsContainer.decodeIfPresent(String.self, forKey: .pointSpreadAwayLine) ?? "N/A"
        totalNumber = try oddsContainer.decodeIfPresent(String.self, forKey: .totalNumber) ?? "N/A"
        overLine = try oddsContainer.decodeIfPresent(String.self, forKey: .overLine) ?? "N/A"
        underLine = try oddsContainer.decodeIfPresent(String.self, forKey: .underLine) ?? "N/A"
        drawLine = try oddsContainer.decodeIfPresent(String.self, forKey: .drawLine) ?? "N/A"
        lastUpdated = try oddsContainer.decodeIfPresent(String.self, forKey: .lastUpdated) ?? "N/A"
        oddType = try oddsContainer.decodeIfPresent(String.self, forKey: .oddType) ?? "N/A"
    }
}

extension GameInformation {
    enum CodingKeys: String, CodingKey {
        case homeTeam = "HomeTeam"
        case awayTeam = "AwayTeam"
        case sport = "Sport"
        case matchTime = "MatchTime"
        case odds = "Odds"
        case details = "Details"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        homeTeam = try container.decodeIfPresent(String.self, forKey: .homeTeam) ?? "N/A"
        awayTeam = try container.decodeIfPresent(String.self, forKey: .awayTeam) ?? "N/A"
        sport = try container.decodeIfPresent(Int.self, forKey: .sport) ?? 20
        matchTime = try container.decodeIfPresent(String.self, forKey: .matchTime) ?? "N/A"
        details = try container.decodeIfPresent(String.self, forKey: .details) ?? "N/A"
        odds = [try Odds(from: decoder)]
    }
}

任何幫助深表感謝!

嘗試在GameInformation init中設置如下賠率:

odds = try container.decode(Array<Odds>.self, forKey: .odds)

接下來,您應該刪除OddsKey枚舉並更新Odds init:

init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)

    moneyLineHome = try container.decodeIfPresent(String.self, forKey: .moneyLineHome) ?? "N/A"
    moneyLineAway = try container.decodeIfPresent(String.self, forKey: .moneyLineAway) ?? "N/A"
    pointSpreadHome = try container.decodeIfPresent(String.self, forKey: .pointSpreadHome) ?? "N/A"
    pointSpreadAway = try container.decodeIfPresent(String.self, forKey: .pointSpreadAway) ?? "N/A"
    pointSpreadHomeLine = try container.decodeIfPresent(String.self, forKey: .pointSpreadHomeLine) ?? "N/A"
    pointSpreadAwayLine = try container.decodeIfPresent(String.self, forKey: .pointSpreadAwayLine) ?? "N/A"
    totalNumber = try container.decodeIfPresent(String.self, forKey: .totalNumber) ?? "N/A"
    overLine = try container.decodeIfPresent(String.self, forKey: .overLine) ?? "N/A"
    underLine = try container.decodeIfPresent(String.self, forKey: .underLine) ?? "N/A"
    drawLine = try container.decodeIfPresent(String.self, forKey: .drawLine) ?? "N/A"
    lastUpdated = try container.decodeIfPresent(String.self, forKey: .lastUpdated) ?? "N/A"
    oddType = try container.decodeIfPresent(String.self, forKey: .oddType) ?? "N/A"
}

我沒有測試過,但是應該可以。 問題是,我們應該將賠率解析為init內的單個對象,並將其解析為GameInformation init內的集合。 我希望這是有道理的。

我認為最好是在解析后進行過濾,您可以在GameInformation init中初始化這樣的賠率(但如果不使用可選參數,這似乎是不正確的):

odds = try container.decode(Array<Odds>.self, forKey: .odds).filter {
        let mirror = Mirror(reflecting: $0)
        return !mirror.children.contains(where: { _, value in
            if let v = value as? String, v == "N/A" {
                return true
            } else {
                return false
            }
        })
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM