【问题标题】:How do I parse this JSON in swift? Object starting with a number [duplicate]如何快速解析这个 JSON?以数字开头的对象[重复]
【发布时间】:2020-12-25 04:01:17
【问题描述】:

所以我能够解析除“1d”对象之外的所有内容。

我怎样才能解析这个?

我正在使用可解码结构和 JSONDecoder/URLSession。

[
  {
    "id": "BTC",
    "currency": "BTC",
    "symbol": "BTC",
    "name": "Bitcoin",
    "price": "23534.92820166",
    "1d": {
      "volume": "35998036640.38",
      "price_change": "533.60149906",
      "price_change_pct": "0.0232",
      "volume_change": "-15387650809.09",
      "volume_change_pct": "-0.2995",
      "market_cap_change": "9934352701.55",
      "market_cap_change_pct": "0.0232"
    }
  }
]

TIA!!

【问题讨论】:

    标签: json swift object parsing numbers


    【解决方案1】:

    它只是一个内部结构。至于名称,只需使用 CodingKeys。仅作为概念证明,假设 "1d""volume" 是我们唯一感兴趣的键:

    struct Outer : Decodable {
        let the1d: Inner
        enum CodingKeys: String, CodingKey {
            case the1d = "1d"
        }
    }
    struct Inner : Decodable {
        let volume: String
    }
    let result = try! JSONDecoder().decode([Outer].self, from: d)
    

    你可以从那里拿走它。

    【讨论】:

    • 非常感谢!!我已经坚持了几天了。
    猜你喜欢
    • 2019-03-22
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    相关资源
    最近更新 更多