在 SWIFT 4 中解析嵌套的 JSON

Posted

技术标签:

【中文标题】在 SWIFT 4 中解析嵌套的 JSON【英文标题】:Parsing Nested JSON in SWIFT 4 【发布时间】:2018-08-06 04:33:08 【问题描述】:

我目前在 SWIFT 4 中使用 Decodable 解析 JSON。

JSON 格式如下:

  
  "autopayout_from": "1.010",
  "earning_24_hours": "9.74731104",
  "error": false,
  "immature_earning": 0.635593030875,
  "last_payment_amount": "1.91238210",
  "last_payment_date": "Mon, 26 Feb 2018 15:08:02 GMT",
  "last_share_date": "Mon, 26 Feb 2018 16:16:01 GMT",
  "payout_daily": false,
  "payout_request": false,
  "total_hashrate": 109006.86,
  "total_hashrate_calculated": 143855.75,
  "transferring_to_balance": 0.2281390807,
  "wallet": "0xbb76fc2ce36a19da28fd713e350a42f1023e2f7f",
  "wallet_balance": "0.49556201",
  "workers": 
    "10003": 
      "alive": false,
      "hashrate": 0.0,
      "hashrate_below_threshold": false,
      "hashrate_calculated": 0.0,
      "last_submit": "Mon, 26 Feb 2018 13:23:16 GMT",
      "second_since_submit": 10612,
      "worker": "10003"
    ,
    "100151": 
      "alive": false,
      "hashrate": 0.0,
      "hashrate_below_threshold": false,
      "hashrate_calculated": 0.0,
      "last_submit": "Mon, 26 Feb 2018 09:30:30 GMT",
      "second_since_submit": 24578,
      "worker": "100151"
    ,
    "100205": 
      "alive": true,
      "hashrate": 19.28,
      "hashrate_below_threshold": true,
      "hashrate_calculated": 24.85,
      "last_submit": "Mon, 26 Feb 2018 16:12:02 GMT",
      "second_since_submit": 486,
      "worker": "100205"
    ,

我可以使用我的 Struct 轻松解析诸如 last_payment_ammount 或 last_payment_date 之类的内容:

struct ticker: Codable
    let error: Bool
    let wallet: String
    let earning_24_hours: String
    let immature_earning: Double
    let last_payment_amount: String
    let last_payment_date: String
    let total_hashrate: Double
    let total_hashrate_calculated: Double
    let wallet_balance: String

我真正难以理解的是获取嵌套的东西。

我如何获取例如使用“worker”变量的工作人员列表,例如可以放入数组中。还是解析一个worker的算力值?

非常感谢

【问题讨论】:

试试QuickType QuickType 似乎很有用:) 【参考方案1】:

这是我将如何建模:

struct Ticker: Codable 
    let autopayoutFrom, earning24_Hours: String
    let error: Bool
    let immatureEarning: Double
    let lastPaymentAmount, lastPaymentDate, lastShareDate: String
    let payoutDaily, payoutRequest: Bool
    let totalHashrate, totalHashrateCalculated, transferringToBalance: Double
    let wallet, walletBalance: String
    let workers: [String: Worker]

    enum CodingKeys: String, CodingKey 
        case autopayoutFrom = "autopayout_from"
        case earning24_Hours = "earning_24_hours"
        case error
        case immatureEarning = "immature_earning"
        case lastPaymentAmount = "last_payment_amount"
        case lastPaymentDate = "last_payment_date"
        case lastShareDate = "last_share_date"
        case payoutDaily = "payout_daily"
        case payoutRequest = "payout_request"
        case totalHashrate = "total_hashrate"
        case totalHashrateCalculated = "total_hashrate_calculated"
        case transferringToBalance = "transferring_to_balance"
        case wallet
        case walletBalance = "wallet_balance"
        case workers
    


struct Worker: Codable 
    let alive: Bool
    let hashrate: Double
    let hashrateBelowThreshold: Bool
    let hashrateCalculated: Double
    let lastSubmit: String
    let secondSinceSubmit: Int
    let worker: String

    enum CodingKeys: String, CodingKey 
        case alive, hashrate
        case hashrateBelowThreshold = "hashrate_below_threshold"
        case hashrateCalculated = "hashrate_calculated"
        case lastSubmit = "last_submit"
        case secondSinceSubmit = "second_since_submit"
        case worker
    


// usage examples:

let ticker = try JSONDecoder().decode(Ticker.self, from: data)
let workerKeys = ticker.workers.keys // "10003", "100151", "100205"
let workers = ticker.workers.values  // all workers objects

let alive = workers.filter  $0.alive  // all workers where alive==true
let totalHashrate = alive.reduce(0.0)  $0 + $1.hashrateCalculated 

【讨论】:

好东西,谢谢,与我现有的代码完美配合。我会尝试解析出一些哈希率等:) @confusedChris 记得点赞并接受答案:) @Cesare 我确实会:) 您介意发布一个解析一些工作人员数据的示例吗? 绝对完美。太感谢了。有道理:)

以上是关于在 SWIFT 4 中解析嵌套的 JSON的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 解析 JSON 嵌套问题

如何解析嵌套的json?迅速

IOS/Swift/JSON:使用 swiftyJSON 解析嵌套的 JSON

如何使用 Codable 和 Swift 解析这个嵌套的 JSON?

在 swift 4 和 Xcode 9 中解码 ExpandableTableView 的嵌套 json 数组

如何在 swift 4 中使用 JSONDecoder 从嵌套 JSON 中获取数据?