Swift 解码 JSON 中的唯一键或忽略某些键

Swift Decode unique keys in JSON or ignore certain keys

我正在练习我的 Swift 技能,这次我试图制作一个 Covid-19 追踪器,为此我找到了这个 API,问题是,格式/cases 检索到的是这样的(更改键以使其更具可读性)

{
    "Country1": {
        "All": {
            "property1": 0,
            "property2": "foo"
        }
    }, {
        "All": {
            "property1": "0",
            "property2": "bar",
        },
        "State1": {
            "property1": 0,
            "property3": "foobar"
        }
    }
}

我做了以下结构来解码它:

国家/地区

struct Country: Codable {
    let All: [String: All]
    
    private enum CodingKeys: String, CodingKey {
        case All
    }
}

全部

struct All: Codable {
    let confirmed: Int?
    let recovered: Int?
    let deaths: Int?
    let country: String?
    let population: Int?
    let squareKmArea: Int?
    let lifeExpectancy: String?
    var elevationInMeters: String?
    let continent: String?
    let location: String?
    let iso: String?
    let capitalCity: String?
    let lat: String?
    let long: String?
    let updated: String?
    
    private enum CodingKeys: String, CodingKey {
        case confirmed
        case recovered
        case deaths
        case country
        case population
        case squareKmArea = "sq_km_area"
        case lifeExpectancy = "life_expectancy"
        case elevationInMeters = "elevation_in_meters"
        case continent
        case location
        case iso
        case capitalCity = "capital_city"
        case lat
        case long
        case updated
    }
    
    init(confirmed: Int?, recovered: Int?, deaths: Int?, country: String?, population: Int?,
         squareKmArea: Int?, lifeExpectancy: String?, elevationInMeters: String?,
         continent: String?, location: String?, iso: String?, capitalCity: String?,
         lat: String?, long: String?, updated: String?) {
        self.confirmed = confirmed
        self.recovered = recovered
        self.deaths = deaths
        self.country = country
        self.population = population
        self.squareKmArea = squareKmArea
        self.lifeExpectancy = lifeExpectancy
        self.elevationInMeters = elevationInMeters
        self.continent = continent
        self.location = location
        self.iso = iso
        self.capitalCity = capitalCity
        self.lat = lat
        self.long = long
        self.updated = updated
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        self.confirmed = try container.decodeIfPresent(Int.self, forKey: .confirmed)
        self.recovered = try container.decodeIfPresent(Int.self, forKey: .recovered)
        self.deaths = try container.decodeIfPresent(Int.self, forKey: .deaths)
        self.country = try container.decodeIfPresent(String.self, forKey: .country)
        self.population = try container.decodeIfPresent(Int.self, forKey: .population)
        self.squareKmArea = try container.decodeIfPresent(Int.self, forKey: .squareKmArea)
        self.lifeExpectancy = try container.decodeIfPresent(String.self, forKey: .lifeExpectancy)
        self.elevationInMeters = try container.decodeIfPresent(String.self, forKey: .elevationInMeters)
        self.continent = try container.decodeIfPresent(String.self, forKey: .continent)
        self.location = try container.decodeIfPresent(String.self, forKey: .location)
        self.iso = try container.decodeIfPresent(String.self, forKey: .iso)
        self.capitalCity = try container.decodeIfPresent(String.self, forKey: .capitalCity)
        self.lat = try container.decodeIfPresent(String.self, forKey: .lat)
        self.long = try container.decodeIfPresent(String.self, forKey: .long)
        self.updated = try container.decodeIfPresent(String.self, forKey: .updated)
        
        do {
            self.elevationInMeters = try String(container.decodeIfPresent(Int.self, forKey: .elevationInMeters) ?? 0)
        } catch DecodingError.typeMismatch {
            print("Not a number")
            self.elevationInMeters = try container.decodeIfPresent(String.self, forKey: .elevationInMeters) ?? ""
        }
    }
}

以米为单位的高程可以是字符串或整数值(4 位以上长 > 字符串(附加逗号),否则为整数)(想法来自

然后我试着这样解码

if let decodedData = self.jsonUtilities.decode(json: safeData, as: [String : Country].self) {
    print(decodedData)
}

我上面的 decode 方法是这样的:

func decode<T: Decodable>(json: Data, as clazz: T.Type) -> T? {
    do {
        let decoder = JSONDecoder()
        let data = try decoder.decode(T.self, from: json)
        
        return data
    } catch {
        print(error)
        print("An error occurred while parsing JSON")
    }
    
    return nil
}

我发现 他们建议在那里添加所有编码密钥,但这些是一大堆。

我没有添加所有密钥,因为它有 8k+ 行 JSON,所以我想知道是否有更简单的解码方法,因为我想不出更好的解码方法这个 JSON 具有唯一键。

或者,如果我可以忽略所有不是 "All" 的键也可能有效,因为我只是想获取每个国家/地区的总数及其位置以将它们放置在地图中。

到目前为止我得到这个错误:

typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Diamond Princess", intValue: nil), CodingKeys(stringValue: "All", intValue: nil), _JSONKey(stringValue: "recovered", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a number instead.", underlyingError: nil))
An error occurred while parsing JSON

据我所知,这是因为它没有找到密钥“钻石公主”,这是加拿大的一个州(或者我相信)(根据我的 JSON),因为我还没有为以上原因。

当你有动态键时,你可以将它解码为 [String: ...] 字典。

JSON的结构如下:

{
  "Canada": {
     "All": { ... },
     "Ontario": { ... },
    ...
  },
  "Mexico": { ... },
   ...
}

所有国家/地区统计数据都有关键字 "All",如果这就是您所需要的,那么您可以创建以下结构:

struct Country: Decodable {
    var all: CountryAll
    enum CodingKeys: String, CodingKey {
        case all = "All"
    }
}

struct CountryAll: Decodable {
    var confirmed: Int
    var recovered: Int
    //.. etc
}

并解码为:

let countries = try JSONDecoder().decode([String: Country].self, from: json)