【问题标题】:Swift 5 How do we parse a Dictionary nested inside a Dictionary, nested inside another Dictionary?Swift 5 我们如何解析嵌套在字典中的字典,嵌套在另一个字典中?
【发布时间】:2021-02-08 07:58:46
【问题描述】:

这是手头的 json。我能够解析除“clientDetails”字典之外的所有数据。这是我的代码。我只是不确定如何映射“clientDetails”字典。我应该采取完全不同的方法吗?还是我在正确的轨道上..

{
  "data": {
    "userID": "124",
    "techID": "1233",
    "clientID": "1",
    "Name": "Haibert",
    "lastName": "Chiem",
    "emailAddress": "haibert@simplygood.com",
    "additionalCompanies": "",
    "clientDetails": {
      "clientID": "1",
      "FirstName": "Sam",
      "LastName": "Ka",
      "email": "rem@simplygood.com",
      "CustomerName": "SimplyRem",
      "BusinessAddress": "9863 N. Glenoaks Blvd, Ste. 207F",
      "BusinessCity": "Burbank",
      "BusinessState": "",
      "BusinessZip": "91502",
      "BusinessCountry": "USA",
      "BusinessPhone": "(818)457-9507",
      "BusinessFax": null,
      "MobilePhone": "818-378-9507",
      "ContactName": "Samuel Kaz",
      "ContactTitle": "Owner",
      "ContactPhone": "818-624-9507",
      "ContactEmail": "sam@simplygood.com",
      "lastUpdated": "2020-10-24 15:09:17"
    }
  }
}

struct UserInformation: Decodable {
    var userID: String = ""
    var clientID: String = ""
    var Name: String = ""
    var lastName: String = ""
    var emailAddress: String = ""
    var techID: String = ""
    var additionalCompanies: String = ""
    
    private enum DataKeys: String, CodingKey {
        case userID = "userID"
        case clientID = "clientID"
        case Name = "Name"
        case lastName = "lastName"
        case emailAddress = "emailAddress"
        case techID = "techID"
        case additionalCompanies = "additionalCompanies"
    }
    
    private enum DataResponseKeys: String, CodingKey {
        case data
    }
    
    init(from decoder: Decoder) throws {
        if let dataResponseContainer = try? decoder.container(keyedBy: DataResponseKeys.self) {
            if let dataContainer = try? dataResponseContainer.nestedContainer(keyedBy: DataKeys.self, forKey: .data) {
                self.userID = try dataContainer.decode(String.self, forKey: .userID)
                self.clientID = try dataContainer.decode(String.self, forKey: .clientID)
                self.Name = try dataContainer.decode(String.self, forKey: .Name)
                self.lastName = try dataContainer.decode(String.self, forKey: .lastName)
                self.emailAddress = try dataContainer.decode(String.self, forKey: .emailAddress)
                self.techID = try dataContainer.decode(String.self, forKey: .techID)
                self.additionalCompanies = try dataContainer.decode(String.self, forKey: .additionalCompanies)
            }
        }
    }
}

【问题讨论】:

    标签: ios swift5 xcode11 xcode12


    【解决方案1】:

    试试下面的数据模型

    let userInformation = try? JSONDecoder().decode(UserInformation.self, from: jsonData)
    
    import Foundation
    
    // MARK: - UserInformation
    struct UserInformation: Codable {
        let data: DataClass
    }
    
    // MARK: - DataClass
    struct DataClass: Codable {
        let userID, techID, clientID, name: String
        let lastName, emailAddress, additionalCompanies: String
        let clientDetails: ClientDetails
    
        enum CodingKeys: String, CodingKey {
            case userID, techID, clientID
            case name = "Name"
            case lastName, emailAddress, additionalCompanies, clientDetails
        }
    }
    
    // MARK: - ClientDetails
    struct ClientDetails: Codable {
        let clientID, firstName, lastName, email: String
        let customerName, businessAddress, businessCity, businessState: String
        let businessZip, businessCountry, businessPhone: String
        let businessFax: String? // null value
        let mobilePhone, contactName, contactTitle, contactPhone: String
        let contactEmail, lastUpdated: String
    
        enum CodingKeys: String, CodingKey {
            case clientID
            case firstName = "FirstName"
            case lastName = "LastName"
            case email
            case customerName = "CustomerName"
            case businessAddress = "BusinessAddress"
            case businessCity = "BusinessCity"
            case businessState = "BusinessState"
            case businessZip = "BusinessZip"
            case businessCountry = "BusinessCountry"
            case businessPhone = "BusinessPhone"
            case businessFax = "BusinessFax"
            case mobilePhone = "MobilePhone"
            case contactName = "ContactName"
            case contactTitle = "ContactTitle"
            case contactPhone = "ContactPhone"
            case contactEmail = "ContactEmail"
            case lastUpdated
        }
    }
    

    如需参考,请遵循
    encoding_and_decoding_custom_types Apple documentation

    的文档

    raywenderlich encoding-and-decoding-in-swift

    【讨论】:

    • 我现在还有一个小问题。为了解码,我必须像这样初始化每个变量.. var userID: String? = "" var clientID: String? = "" 但对于 clientDetails 我不确定如何初始化,因为它是 Codable 结构。 var clientDetails: ClientDetails = ClientDetails.init(from: Decoder) 初始化它的正确方法是什么?
    • @haibert 我不确定你为什么需要它作为 var,如果它可以为空,那么你可以简单地使用可选的。例如字符串?而不是字符串。如果您可以添加项目的 github 链接,我可能会对您要实现的目标有更多的了解。如果您需要参考github.com/hadanischal/NearbyHospitals,我很少有带有 Codable 示例的开源项目。
    • 我需要将其更改为 var 并对其进行初始化,以便我可以使用下面的代码来解码 json。这是否提供了足够的上下文? init(from decoder: Decoder) throws { if let dataContainer = try? decoder.container(keyedBy: CodingKeys.self) { self.userID = try dataContainer.decode(String.self, forKey: .userID) }
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2019-05-02
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多