ios - 为键为值的 JSON 创建模型

标签 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,
            "__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中与其key对应的记录。

我正在使用 AlamofireObjectMapper 创建模型,这里是 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"]
    }  
}  

问题:假设将来 pin_type 值 - 1、4、101、104 在后端发生变化,我如何在不更改模型的情况下处理这种情况.根据这个模型结构,每次后端模型更改时我都需要更改我的模型类

最佳答案

下面是如何使用 Codable 作为解决方案,

1. 创建一个模型 Row,它将在 jsonrows 数组中包含单个行的数据>,即

class Row: Decodable {
    var id: String?
    var pinType: String?
    var pinId: Int?

    enum CodingKeys: String, CodingKey {
        case id = "_id"
        case pinId = "pin_type"
    }
}

在上面的模型中,我使用了 2 个不同的属性 - pinType 和 pinId

  1. pinId 将在 row

  2. 中包含 pin_type
  3. pinType 将包含对应于 pinId 的实际值。我们稍后会填充此值。

另外,我只使用了的一小部分键。您可以根据需要添加更多内容。

2. 接下来创建另一个模型 Response,它将包含 Rowarray,即

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)]
            }
        })
    }
}

在上面的模型中,

  1. rows 数组在 json 中被解析为 [Row]

  2. pinTypeText dictionary 被解析为 [String:String] 类型。

  3. [Row] 被枚举以在每个 row 中使用 pinId 填充 pinType >pinTypeText 字典

使用时需要使用Row对象的pinType属性。

response?.rows?.forEach({ print($0.pinType) }) //This line will print - "In Fingerprint" and "Out Fingerprint"

如果您在实现此方法时遇到问题,请告诉我。

关于ios - 为键为值的 JSON 创建模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56232758/

相关文章:

ios - UIButton 如何同时监听对其他按钮的更改、对 UILabel 文本的更改以及对 UITextView 的更改

javascript - 如何以blow json格式编写多个嵌套的ng-repeat

c++ - 如何从 Swift 中的整数中获取特定位?

javascript - iOS 从 Javascript 调用 Objective-C : Some calls are ignored?

javascript - 在 keyup 函数运行时保持 iOS 键盘打开

ios - 通过 iphone 上的应用程序创建日历(在 ios native 日历中)

java - 我无法在应用程序中读取 JSON

arrays - 拆分字符串数组

ios - 从 parse.com 检索对象并等待返回直到检索到数据

ios - "Missing argument for parameter ' 样式' in call"创建系统UIBarButtonItem 错误