解析可编码类并避免重复 [英] Parse Codable classes and avoid repetition
问题描述
我有如下JSON响应:
I have a JSON response as the following:
{
"http_status": 200,
"success": true,
"has_error": false,
"error": [
""
],
"response": {
"token": "",
"verified": false,
"message": ""
}
}
据我所讲,应用程序API使用情况http_status,成功,has_error,错误在所有APIS之间共享,因此我将创建一个Codable类来处理
As far as i can say for an app API usage http_status, success, has_error, error are shared between all APIS and thus i will create a Codable class to handle it, but the response could be different model, so here is what I'm trying to do.
我创建了如下所示的通用响应类,因此该类是我要尝试的方法。我可以使用该项目中的所有api来避免重复相同的类但使用不同的名称:
I have created a general response class as the below, so this class i can use for all apis in the project to avoid duplication of same class but different names:
class GeneralResponse:Codable {
let http_status: Int?
let success, has_error: Bool?
let error: [String]?
enum CodingKeys: String, CodingKey {
case http_status = "http_status"
case success = "success"
case has_error = "has_error"
case error = "error"
}
init(http_status: Int?, success: Bool?, has_error: Bool?,error: [String]?) {
self.http_status = http_status
self.success = success
self.has_error = has_error
self.error = error
}
}
现在我创建了响应类,该类现在将处理注册响应:
Now i have created the response class which will handle for now the registration response:
class RegistrationResponseDetails: Codable {
let token: String?
let verified: Bool?
let message: String?
init(token: String?, verified: Bool?, message: String?) {
self.token = token
self.verified = verified
self.message = message
}
}
说我需要解析注册响应,所以这是我的工作,我创建了一个类并使用了它们:
And lets say i need to parse the registration the response so here is what i did, i have created a class and used both of them:
class RegistrationResponse: Codable {
let generalResponse:GeneralResponse?
let response: RegistrationResponseDetails?
init(generalResponse: GeneralResponse?, response: RegistrationResponseDetails?) {
self.generalResponse = generalResponse
self.response = response
}
}
所以我将主要使用RegistrationResponse来解析响应,该响应将解析 generalResponse,其中包括http_status,成功, has_error,错误,然后响应将解析所需的响应对象。
So i will mainly use RegistrationResponse to parse the response which will parse "generalResponse" which includes http_status, success, has_error, error, and then response will parse the desired response object.
但是在某些时候,generalResponse对象始终为零,并且响应具有正确解析的数据,我该怎么办使得在每个api中对generalResponse进行解析而不重复,因为在每个api中我都会有generalResponse对象,因此有可能解决这个问题吗?
But at some point generalResponse object is always nil and response has the data parsed correctly, what should i do to make generalResponse get parsed without duplication in each api, because in each api i will have generalResponse object so is it possible to solve it ?
注意:我正在使用Alamofire作为网络库。
Note: I'm using Alamofire as the networking library.
推荐答案
您可以创建 GeneralResponse
通用,并告诉它解析响应时使用什么类型:
You can make your GeneralResponse
generic and tell it what type to use when parsing the response:
class GeneralResponse<T: Codable>: Codable {
let http_status: Int?
let success, has_error: Bool?
let error: [String]?
let response: T?
}
class RegistrationResponseDetails: Codable {
let token: String?
let verified: Bool?
let message: String?
}
然后在解析json时可以给它内部响应类: / p>
Then you can give it the inner response class when you parse the json:
let generalResponse = try JSONDecoder().decode(GeneralResponse<RegistrationResponseDetails>.self, from: jsonData)
// generalResponse.response is of type RegistrationResponseDetails?
这篇关于解析可编码类并避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!