gpt4 book ai didi

json - 在 Swift 中迭代解码 JSON 的字典键

转载 作者:可可西里 更新时间:2023-11-01 01:08:49 25 4
gpt4 key购买 nike

我在上一个关于如何设置基础 JSON 模型的问题中得到了很好的帮助。我能够解析任何我想要的值。

虽然我可以解析我想要的任何值,但我只能使用点符号单独访问符号或其他值。
btcSymbol = rawResponse.btc?.symbol
ethSymbol = rawResponse.eth?.symbol

我发现了关于遍历字典的其他问题,例如 Iterating Through a Dictionary in Swift但这些示例是基本数组,而不是使用 Swift 新协议(protocol)的多嵌套字典。

我希望能够:
1. 遍历 JSON 并从 CMC API 中提取仅符号
2. 有一个模型,我可以在其中分别迭代每种货币的所有值,以便稍后可以将这些值发送到表格 View 。
比特币 |姓名 |符号 |市值 |最大供应
以太币 |姓名 |符号 |市值 |最大供应

重组现有模型是否是最佳解决方案?在我的模型构建好之后,in loop 或 map 的标准会更好吗?

JSON模型

struct RawServerResponse : Codable {
enum Keys : String, CodingKey {
case data = "data"
}
let data : [String:Base]
}

struct Base : Codable {
enum CodingKeys : String, CodingKey {
case id = "id"
case name = "name"
case symbol = "symbol"
}

let id : Int64
let name : String
let symbol : String
}

struct Quote : Codable {
enum CodingKeys : String, CodingKey {
case price = "price"
case marketCap = "market_cap"
}

let price : Double
let marketCap : Double
}

extension RawServerResponse {
enum BaseKeys : String {
case btc = "1"
case eth = "1027"
}
var btc : Base? { return data[BaseKeys.btc.rawValue] }
var eth : Base? { return data[BaseKeys.eth.rawValue] }

}

extension Base {
enum Currencies : String {
case usd = "USD"
}
var usd : Quote? { return quotes[Currencies.usd.rawValue]}
}

struct ServerResponse: Codable {
let btcName: String?
let btcSymbol: String?

init(from decoder: Decoder) throws {
let rawResponse = try RawServerResponse(from: decoder)

btcSymbol = rawResponse.btc?.symbol

JSON

{
"data": {
"1": {
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"website_slug": "bitcoin",
"rank": 1,
"circulating_supply": 17041575.0,
"total_supply": 17041575.0,
"max_supply": 21000000.0,
"quotes": {
"USD": {
"price": 8214.7,
"volume_24h": 5473430000.0,
"market_cap": 139991426153.0,
"percent_change_1h": 0.09,
"percent_change_24h": 2.29,
"percent_change_7d": -2.44
}
}
}

最佳答案

至少我建议映射 data 字典以获取 symbol 作为键而不是 id,顺便说一下,如果 key 是 camelCaseable 并且您传递了 .convertFromSnakeCase key 解码策略您不需要任何编码 key ,例如

struct RawServerResponse : Codable {
var data = [String:Base]()

private enum CodingKeys: String, CodingKey { case data }

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let baseDictionary = try container.decode([String:Base].self, forKey: .data)
baseDictionary.forEach { data[$0.1.symbol] = $0.1 }
}
}

struct Base : Codable {
let id : Int64
let name : String
let symbol : String
let quotes : [String:Quote]
}

struct Quote : Codable {
let price : Double
let marketCap : Double
}

do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let rawResponse = try decoder.decode(RawServerResponse.self, from: data)
for (symbol, base) in rawResponse.data {
print(symbol, base.quotes["USD"]?.marketCap)
// ETH Optional(68660795252.0)
// BTC Optional(139991426153.0)
}
} catch { print(error) }

关于json - 在 Swift 中迭代解码 JSON 的字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50487640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com