一尘不染

在Swift中解析Json

json

我在解析Json文件时遇到问题。尝试解析一个Json文件:

{
  "id": 1234,
  "lists": {
    "pause": {
      "attached": [
        {
          "from": 1576680044000,
          "to": 1576680055000,
          "length": 11000
        }
      ]
    }
  },
  "time": {
    "start_time": 1576680044000,
    "end_time": 1576680055000
  }
}

这是我为其定义的两个结构。

暂停结构:

public struct Pause: Decodable {

   public let attached: [AttachedModel]

   init(badlAttached: [AttachedModel] = []) {
      self.attached = attached
   }

   private enum CodingKeys: String, CodingKey {
      case attached = "attached"
   }
}

AttachedModel模型结构:

public struct AttachedModel: Decodable {

   private enum CodingKeys: CodingKey {
      case from, to
   }

   public let range: Range<Int64>

   init(range: Range<Int64>) {
      self.range = range
   }

   public init(from decoder: Decoder) throws {
      do {
         let rootContainer = try decoder.container(keyedBy: CodingKeys.self)
         let from: Int64 = try rootContainer.decode(key: .from)
         let to: Int64 = try rootContainer.decode(key: .to)
         range = from ..< to
      } catch {
         throw JSONDecoder.DecodingError(type: type(of: self), payload: error)
      }
   }
}

然后,我为其创建一个键和一个函数:

   enum Key: String {
      case lists
      case pause = "pause"
   }

 func pause() throws -> Pause? {
      let seriesJSON = try lists()
      if let json = seriesJSON[Key.pause.rawValue] as? [String: Any] {
         return try JSONDecoder().decode(Pause.self, from: json) //It's never called
      } else {
         return nil
      }
   }

在功能上lists()

func lists() throws -> [String: Any] {
      let json: [String: Any] = try payload.valueForRequiredKey(Key. lists.rawValue)
      return json
   }

这是输出 list()

  [1] = {
    key = "pause"
    value = {
      payload_data_0 = 0x0000600001af8a10 {
        ObjectiveC.NSObject = {
          baseNSObject@0 = {
            isa = __NSSingleObjectArrayI
          }
        }
      }
      payload_data_1 = 0x544e786f6a497768
      payload_data_2 = 0x444f3363544e3363
      instance_type = 0x00007fe02f877ed0
    }
  }

问题是,即使pause有数据,该行也始终为假if let json = seriesJSON[Key.pause.rawValue] as? [String: Any] {,并且转到“ nil 有人可以告诉我这里的问题在哪里吗?


阅读 262

收藏
2020-07-27

共1个答案

一尘不染

为您的json创建此模型类:

import Foundation

class MyItem: Codable {
    let id: Int
    let lists: Lists
    let time: Time
}

class Lists: Codable {
    let pause: Pause
}

class Pause: Codable {
    let attached: [Attached]
}

class Attached: Codable {
    let from, to, length: Int
}

class Time: Codable {
    let start_time, end_time: Int
}

// MARK: - Helper functions for creating encoders and decoders
fileprivate func newJSONDecoder() -> JSONDecoder {
    let decoder = JSONDecoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        decoder.dateDecodingStrategy = .iso8601
    }
    return decoder
}

fileprivate func newJSONEncoder() -> JSONEncoder {
    let encoder = JSONEncoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        encoder.dateEncodingStrategy = .iso8601
    }
    return encoder
}

然后,您可以像这样解析它:

let myItem = try? newJSONDecoder().decode(MyItem.self, from: jsonData)
// Read from (for example)
let from = myItem.lists.pause.attached[0].from
2020-07-27