Ios 创建键为值的JSON模型
JSON:Ios 创建键为值的JSON模型,ios,json,swift,objectmapper,Ios,Json,Swift,Objectmapper,JSON: { "rows" : [ { "_id": "5cdc0ede5c3dcb04bdb3a972", "emp_code": 187, "log_id": 361711, "punch_time": "2019-05-07T04:00:33.000Z", "pin_type": 1, "status": 4,
{
"rows" :
[
{
"_id": "5cdc0ede5c3dcb04bdb3a972",
"emp_code": 187,
"log_id": 361711,
"punch_time": "2019-05-07T04:00:33.000Z",
"pin_type": 1,
"status": 4,
"__v": 0
},
{
"_id": "5cdc40de5c3dcb04bdb3a972",
"emp_code": 111,
"log_id": 361701,
"punch_time": "2019-05-07T04:00:35.000Z",
"pin_type": 101,
"status": 4,
"__v": 0
}
],
"pin_type_text": {
"1": "In Fingerprint",
"4": "In Card",
"101": "Out Fingerprint",
"104": "Out Card"
}
}
每行pin_type的值指的是pin_type_text中与其键映射的记录
我正在使用AlamoFileObject Mapper创建模型,下面是PinTypeText模型:
class PinTypeText : Mappable {
var inFingerprint: String?
var inCard: String?
var outFingerprint: String?
var outCard: String?
required init?(map: Map) {
}
func mapping(map: Map) {
self.inFingerprint <- map["1"]
self.inCard <- map["4"]
self.outFingerprint <- map["101"]
self.outCard <- map["104"]
}
}
class PinTypeText:可映射{
var inFingerprint:字符串?
变量:字符串?
var OUTPINGERPRINT:字符串?
var outCard:字符串?
必需的初始化?(映射:映射){
}
func映射(映射:映射){
self.inFingerprint以下是如何使用Codable
作为解决方案
1.创建一个模型行
,该模型将包含行
json
数组中的一行数据,即
class Row: Decodable {
var id: String?
var pinType: String?
var pinId: Int?
enum CodingKeys: String, CodingKey {
case id = "_id"
case pinId = "pin_type"
}
}
class Response: Decodable {
var rows: [Row]?
enum CodingKeys: String, CodingKey {
case rows, pin_type_text
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
rows = try values.decodeIfPresent([Row].self, forKey: .rows)
let pinTypeText = try values.decodeIfPresent([String:String].self, forKey: .pin_type_text)
rows?.forEach({ (row) in
if let pinId = row.pinId {
row.pinType = pinTypeText?[String(pinId)]
}
})
}
}
在上面的模型中,我使用了两种不同的属性-pinType和pinId
pinId
将在行中包含pin\u类型
值
pinType
将包含与pinId
对应的实际值。我们稍后将填充此值行
的一小部分键。您可以根据需要添加更多键
2.接下来创建另一个模型响应
,该模型将包含行
的数组
,即
class Row: Decodable {
var id: String?
var pinType: String?
var pinId: Int?
enum CodingKeys: String, CodingKey {
case id = "_id"
case pinId = "pin_type"
}
}
class Response: Decodable {
var rows: [Row]?
enum CodingKeys: String, CodingKey {
case rows, pin_type_text
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
rows = try values.decodeIfPresent([Row].self, forKey: .rows)
let pinTypeText = try values.decodeIfPresent([String:String].self, forKey: .pin_type_text)
rows?.forEach({ (row) in
if let pinId = row.pinId {
row.pinType = pinTypeText?[String(pinId)]
}
})
}
}
在上述模型中
json
中的rows
数组被解析为[Row]
pinTypeText
dictionary
被解析为[String:String]
类型[Row]
被枚举以使用pinId
和pinTypeText字典在每个行中填充pinType
行
对象的pinType
属性
response?.rows?.forEach({ print($0.pinType) }) //This line will print - "In Fingerprint" and "Out Fingerprint"
如果您在实现此方法时遇到问题,请告诉我。您可以使用
Codable
容器来解析此类数据。@PGDev:但是对于Codable,我不需要在JSON(即1,4101104)中使用相同的变量名称吗?这是不可能的命名约定。由于您的JSON由于版本迁移而更新,您可能需要ant要创建两个模型来处理不同的API响应JSON,那么ObjectMapper就可以在不同版本中映射,而无问题。例如PintypetText\uV1、PintypText\uV2继承自同一父模型PintypetText,使用不同的映射ObjectMapper@PeterGuo:但这将要求每次发生更改时更新代码在后端,不是吗?@Nitish添加了一个带有codable
的答案。请查看。