【问题标题】:Model from JSON containing Array and Dictionary来自包含数组和字典的 JSON 模型
【发布时间】:2017-04-30 15:25:01
【问题描述】:
class AppUsers: Object{
    dynamic var email: String = ""
    dynamic var type: String = ""

    required convenience init?(map: Map) {
        self.init()
        mapping(map: map)
    }

    override class func primaryKey() -> String {
        return "email"
    }
}

        extension AppUsers : Mappable {

            func mapping(map: Map) {
                email               <- map["email"]
        //        active_platforms    <- map["active_platforms"]
                type                <- map["type"]
        //        linked_to           <- map["linked_to"]
            }
        }

JSON 响应:

{
       "email": "asd@gmail.com",
       "type": "primary_email",
       "linked_to": {
         "_id": "DAS44564dasdDASd",
         "image": null,
         "company": null,
         "designation": null,
         "name": null
       },
       "active_platforms": [
         "asd",
         "qwe"
       ]
     }

从上面的响应如何得到linked_to 这是一个字典和active_platforms 这是一个数组。我尝试在 AppUsers 中为 linked_to create var 创建单独的类,但没有帮助。

【问题讨论】:

    标签: ios swift realm objectmapper


    【解决方案1】:

    一对一的关系是

    dynmamic var linkedTo: LinkObject?
    

    一对多的关系是

    let activePlatforms = List<ActivePlatform>()
    

    如果您想使用对象映射器来填充它们,LinkObject 必须是可映射的,ActivePlatform 必须是可映射的,并且您必须提供 custom transform 以将 JSON 数组转换为领域列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多