iOS Swift中JSON响应结构体重复定义报错及解析问题求助
问题根源
你编写的代码中存在同名类型重复定义的问题:在同一作用域下,你为两个JSON响应分别定义了同名的Response和Result结构体,Swift不允许重复声明相同名称的类型,这会直接导致编译报错。
解决方案
通过给不同响应的结构体添加专属前缀,或者使用嵌套结构体的方式,避免命名冲突。以下是两种可行的修改方案:
方案1:添加专属前缀(推荐,结构清晰)
为每个响应的Response和Result结构体添加对应前缀,明确区分不同业务的模型:
OTP响应模型
// MARK: - OTP响应根结构 struct OTPRes: Codable { let response: OTPResponse } // MARK: - OTP响应内容 struct OTPResponse: Codable { let success: String let result: OTPResult } // MARK: - OTP结果体 struct OTPResult: Codable { let encryptedOtp: String }
登录响应模型
// MARK: - 登录响应根结构 struct LoginRes: Codable { let response: LoginResponse } // MARK: - 登录响应内容 struct LoginResponse: Codable { let success: String let result: LoginResult } // MARK: - 登录结果体 struct LoginResult: Codable { let lastName: String let deviceIDS: [DeviceID] let loginID: String let profileImage: String let platform: String let doNotDisturb: Bool let segment, email: String let editAccess, roaming: Bool let contactID, mobile, accountclass: String let dashboardInfo: DashboardInfo let assignedUserID, accountNumber, userName, accessToken: String let lastLoginTime, accountID, firstName, servicetype: String let pushNotifications: Bool let response, success, basePlan, location: String enum CodingKeys: String, CodingKey { case lastName case deviceIDS = "deviceIds" case loginID = "loginId" case profileImage, platform, doNotDisturb, segment, email, editAccess, roaming case contactID = "contactId" case mobile, accountclass, dashboardInfo case assignedUserID = "assigned_user_id" case accountNumber, userName, accessToken, lastLoginTime case accountID = "accountId" case firstName, servicetype, pushNotifications, response, success, basePlan, location } } // MARK: - 仪表盘信息 struct DashboardInfo: Codable { let noOfServiceAccounts: String } // MARK: - 设备ID struct DeviceID: Codable { let accountName, accountNo, deviceID, email: String enum CodingKeys: String, CodingKey { case accountName, accountNo case deviceID = "deviceId" case email } }
方案2:嵌套结构体
将Response和Result嵌套在根结构体内部,利用Swift的嵌套类型特性避免冲突:
OTP响应模型
struct OTPRes: Codable { struct Response: Codable { let success: String struct Result: Codable { let encryptedOtp: String } let result: Result } let response: Response }
登录响应模型
struct LoginRes: Codable { struct Response: Codable { let success: String struct Result: Codable { let lastName: String let deviceIDS: [DeviceID] let loginID: String let profileImage: String let platform: String let doNotDisturb: Bool let segment, email: String let editAccess, roaming: Bool let contactID, mobile, accountclass: String let dashboardInfo: DashboardInfo let assignedUserID, accountNumber, userName, accessToken: String let lastLoginTime, accountID, firstName, servicetype: String let pushNotifications: Bool let response, success, basePlan, location: String enum CodingKeys: String, CodingKey { case lastName case deviceIDS = "deviceIds" case loginID = "loginId" case profileImage, platform, doNotDisturb, segment, email, editAccess, roaming case contactID = "contactId" case mobile, accountclass, dashboardInfo case assignedUserID = "assigned_user_id" case accountNumber, userName, accessToken, lastLoginTime case accountID = "accountId" case firstName, servicetype, pushNotifications, response, success, basePlan, location } } let result: Result } let response: Response } struct DashboardInfo: Codable { let noOfServiceAccounts: String } struct DeviceID: Codable { let accountName, accountNo, deviceID, email: String enum CodingKeys: String, CodingKey { case accountName, accountNo case deviceID = "deviceId" case email } }
解码示例
修改完成后,即可正常解码对应JSON:
解码OTP响应
// 假设otpJsonData是从网络请求获取的JSON数据 do { let otpResponse = try JSONDecoder().decode(OTPRes.self, from: otpJsonData) print("加密OTP:\(otpResponse.response.result.encryptedOtp)") } catch { print("OTP解码失败:\(error.localizedDescription)") }
解码登录响应
// 假设loginJsonData是从网络请求获取的JSON数据 do { let loginResponse = try JSONDecoder().decode(LoginRes.self, from: loginJsonData) print("用户名:\(loginResponse.response.result.firstName) \(loginResponse.response.result.lastName)") } catch { print("登录响应解码失败:\(error.localizedDescription)") }
内容的提问来源于stack exchange,提问作者user15660516
相关产品推荐
相关产品推荐

