首页 > 解决方案 > 将数据作为字典连接到模型

问题描述

我从服务器获取数据并将它们保存在字典中,Json 数据如下所示:

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

我读取了这些数据并将它们保存在字典中,这是字典的输出:

   ([[String : Any]]?) $R18 = 1 value {
  [0] = 1 key/value pair {
    [0] = {
      key = "attached"
      value = {
        payload_data_0 = 0x0000600002d51b30 3 elements {
          [0] = 0x00006000036d2b80 3 key/value pairs
          [1] = 0x00006000036d32c0 3 key/value pairs
          [2] = 0x00006000036d2400 3 key/value pairs
        }
        payload_data_1 = 0x2f73746375646f72
        payload_data_2 = 0x70692d6775626544
        instance_type = 0x00007fd61984ab70
      }
    }
  }

现在,我想将这些数据连接到模型

这是暂停模型:

public struct Pause: Decodable {

   public let attached: [AttachedModel]

   init(attached: [AttachedModel] = []) {
      attached = attached
   }

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

和附加模型模型:

public struct AttachedModel: Decodable {

   private enum CodingKeys: CodingKey {
      case from, to, len
   }

   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)
      }
   }
}

在网络部分(套接字)中,我为此创建了一个函数:

func pause() throws -> Pause? {
      let lists = try lists()
      if let data = lists["pause"] as? [[String : Any]] {
         return JSONDecoder().decode(Pause.self, from: data)
         //The old form of this return, I think it's wrong and now it shows this error: Cannot invoke 'decode' with an argument list of type '(Pause.Type, from: [[String : Any]])'
      } else {
         return nil
      }

正如我所说,data保存数据字典;这是数据的输出:

 ([[String : Any]]?) $R18 = 1 value {
  [0] = 1 key/value pair {
    [0] = {
      key = "attached"
      value = {
        payload_data_0 = 0x0000600002d51b30 3 elements {
          [0] = 0x00006000036d2b80 3 key/value pairs
          [1] = 0x00006000036d32c0 3 key/value pairs
          [2] = 0x00006000036d2400 3 key/value pairs
        }
        payload_data_1 = 0x2f73746375646f72
        payload_data_2 = 0x70692d6775626544
        instance_type = 0x00007fd61984ab70
      }
    }
  }

请查看退货下方的评论,我添加了之前的公司,但现在错误并且显示错误,谁能帮助我如何将这些数据连接到那些模型?谢谢

标签: jsonswiftnsdictionary

解决方案


如果您只想attached从上述 JSON 中获取数据,您可以创建模型,例如,

struct Root: Decodable {
    let attached: [Attached]

    enum CodingKeys: String, CodingKey {
        case lists, pause, attached
    }

    init(from decoder: Decoder) throws {
        let pause = try decoder.container(keyedBy: CodingKeys.self).nestedContainer(keyedBy: CodingKeys.self, forKey: .lists).nestedContainer(keyedBy: CodingKeys.self, forKey: .pause)
        self.attached = try pause.decode([Attached].self, forKey: .attached)
    }
}

struct Attached: Decodable {
    let to: Int64
    let from: Int64
    var range: Range<Int64> {
        return from..<to
    }
}

现在,使用解析数据

do {
    let response = try JSONDecoder().decode(Root.self, from: data)
    print(response.attached)
} catch {
    print(error)
}

编辑:

有效的JSON

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

推荐阅读