简体   繁体   English

用于 Alamofire responseDecodable 的数组结构

[英]Struct with array for Alamofire responseDecodable

I'm struggling to setup a struct correctly for Alamofire responseDecodable to be decoded.我正在努力为要解码的 Alamofire responseDecodable 正确设置结构。

My JSON return structure is:我的 JSON 返回结构是:

{
“SESSIONID” : ”GUID”, 
“ISADMIN” : ”YES or NO”, 
“FNAME” : ”ABC”, 
“SNAME” : ”ABC”, 
“EMPNO” : ”ABC”, 
"SITES": [
    {
    "NAME": “MTN-ALICE LANE”,
    "WEBSITEAPPID": “SiteGUID”
    }
  ]
}

My Swift code is where the issue is under the array SITES.我的 Swift 代码是问题出在数组 SITES 下的地方。 I know this because if I remove the SITES from my struct then the rest of the JSON response accordingly but without the SITES array.我知道这一点,因为如果我从我的结构中删除 SITES,那么相应的 JSON 响应的其余部分将不包含 SITES 数组。

struct ValidateUser: Decodable{
    
    let sessionId: String?
    let isAdmin: String?
    let fullName: String?
    let surname: String?
    let employNo: String?
    let siteNames: [UserSites]
    
    enum CodingKeys: String, CodingKey {
        case sessionId  = "SESSIONID"
        case isAdmin    = "ISADMIN"
        case fullName   = "FNAME"
        case surname    = "SNAME"
        case employNo   = "EMPNO"
        case siteNames  = "SITES"
    }
}

struct UserSites: Decodable{
    
    let siteName: String?
    let siteId: String?
    
    enum CodingKeys: String, CodingKey {
        case siteName   = "NAME"
        case siteId     = "WEBSITEAPPID"
    }
}

I assume my error is how I've created struct UserSites but unsure.我假设我的错误是我如何创建 struct UserSites 但不确定。

Missing option?缺少选项? after let siteNames: [UserSites]在 let siteNames: [UserSites] 之后

let siteNames: [UserSites]?让 siteNames: [UserSites]?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM