在完成中添加数据模型字典:转义

我有一个从服务器获取的数据的数据模型

它的一部分(因为它很长,所以我只复制了一部分)

public struct ExportUserDataModel: Decodable {

   public let username: String
   public let id: Int
   public let data: [Int]

...

Then, we use a custom form of NSURLSeassonto fetch Data

 public func fetchtExportCSV(id: Int, completion: @escaping Result<[ExportUserDataModel]>.Completion) {
      let request = userRequest.export(id: id)
       requestProvider.execute([ExportUserDataModel].self, request: request, on: .main) { result in
          switch result {
          case .success(let object):
             completion(.success(object))
          case .error(let error):
             logger.error(.response, error)
             completion(.error(error))
          }
       }
    }

在这里,如果数据进入数组,则可以正常工作。但知道,数据来自字典 这样的东西

{
  "username" : "Dubbeloo",
  "id" : 12869,
  "data" : [
    [
      1904
    ],
    [
      1928
    ]
}

my problem is, how can I use add the data in the fetch function to get the dictionary, now it's array [ExportUserDataModel] and [ExportUserDataModel].self

谢谢您的帮助

评论
  • 十梦九你
    十梦九你 回复

    改变这个

    requestProvider.execute([ExportUserDataModel].self, request: request, on: .main) { result in
    

    requestProvider.execute(ExportUserDataModel.self, request: request, on: .main) { result in
    

    如果您需要两者都支持,请检查数据转换结果或

    do {
          // decode an array 
       }
        catch {
           // decode a dictionary 
        }