Swift 将 Web 请求中的数据存储到结构中

Swift store data from a Webrequest into structures

首先我真的是 Swift 的初学者。我在这上面卡了几个小时。

这是问题所在,我正在使用 AlamoFire 来提出我的请求

func getListFiles(userId: String, page: Int)
{
    let parameters: Parameters = [
        "userId": userId,
        "page": page
    ]

    Alamofire.request(baseUrl + Endpoints.GET_RECORDS.rawValue, method: HTTPMethod.post, parameters: parameters)
        .responseData { response in
            if let result = response.result.value{
                do {
                    let data = try JSONDecoder().decode(ListFilesStruct.self, from: result)
                } catch {
                    print("\(error)")
                }
            }
    }
}

并且我想在我的结构 "ListFilesStruct" 中存储数据响应。但它进入 "catch" 并失败

Printing description of error:
▿ DecodingError
  ▿ typeMismatch : 2 elements
- .0 : Swift.Dictionary<Swift.String, Any>
▿ .1 : Context
  ▿ codingPath : 3 elements
    - 0 : CodingKeys(stringValue: "data", intValue: nil)
    ▿ 1 : _JSONKey(stringValue: "Index 6", intValue: 6)
      - stringValue : "Index 6"
      ▿ intValue : Optional<Int>
        - some : 6
    - 2 : CodingKeys(stringValue: "expert", intValue: nil)
  - debugDescription : "Expected to decode Dictionary<String, Any> but found a string/data instead."
  - underlyingError : nil

这里是主要结构,我想要一个FileDetail数组

struct ListFilesStruct: Codable
{
    var success: Bool?
    var data: [FileDetail]?
}

struct FileDetail: Codable
{
    var id: String?
    var expert: FileElement?
}

而且由于 FileElement 结构,它完全失败了,我不知道为什么

struct FileElement: Codable
{
    var id: String?
    var role_id: String?
    var avatar: String?
    var nom: String?
    var prenom: String?  
}

我真的很想像那样存储 Web 服务数据,谢谢

编辑:预期JSON:

{
"success": true,
"data": [
    {
        "id": "A19007994",
        "expert": {
            "id": "74EJEEZM",
            "role_id": "EXPERT",
            "avatar": null,
            "nom": "METRTALZ",
            "prenom": "JEREMIE",
        }
     }
 ]
 }

根据您的预期使用以下结构 json

class MyCustomJSON: Codable {
    let success: Bool
    let data: [Datum]

    enum CodingKeys: String, CodingKey {
        case success = "success"
        case data = "data"
    }

    init(success: Bool, data: [Datum]) {
        self.success = success
        self.data = data
    }
}

class Datum: Codable {
    let id: String
    let expert: Expert

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case expert = "expert"
    }

    init(id: String, expert: Expert) {
        self.id = id
        self.expert = expert
    }
}

class Expert: Codable {
    let id: String
    let roleID: String
    let nom: String
    let prenom: String

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case roleID = "role_id"
        case nom = "nom"
        case prenom = "prenom"
    }

    init(id: String, roleID: String, nom: String, prenom: String) {
        self.id = id
        self.roleID = roleID
        self.nom = nom
        self.prenom = prenom
    }
}

// MARK: Encode/decode helpers

class JSONNull: Codable, Hashable {

    public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
        return true
    }

    public var hashValue: Int {
        return 0
    }

    public init() {}

    public required init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if !container.decodeNil() {
            throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
        }
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encodeNil()
    }
}

并像这样初始化

do {
     let myCustomJSON = try JSONDecoder().decode(MyCustomJSON.self, from: jsonData)
} catch {
  print("\(error)")
}

我在操场上尝试了我的代码,一切正常。