iOS JSON 转 Model

简要步骤

将JSON转成对象分一下两步:

  1. 将原始数据格式转化JSON object, 如字典
  2. 将JSON object映射到Model

例如有JSON数据

{
    "name" : "mike",
    "age" : 26,
    "address" : "Beijin",
}

我们建立model如下

public class MemberModel: NSObject {
    public var name:String = ""
    public var age:Int?
    public var address:String?
}

使用Extension两行代码即可实现转换

let jsonObject = data.jsonObject
let model = MemberModel(JSONDict:jsonObject)

映射问题

有时候后端传递的JSON字段并不冷直接拿来用,如下

{
    "name" : "mike",
    "age" : 26
    "id" : 3344,
    "mobile_phone" : 18688888888,
}

其中id是系统占用的名字, mobile_phone不符合Objective-C明明风格,不能来直接所谓属性的名字,需要换一下,如用uid和mobildPhone代替

public class MemberModel: NSObject {
    public var name:String = ""
    public var age:Int?
    public var address:String?
    public var uid = 0
    public var mobilePhone: String?
}

这样的MemberModel(JSONDict:jsonObject)中setvalue for key将对不上, 需要override函数如下

func setValue(value: AnyObject!, forUndefinedKey key: String) {
    if key = "id" {
        self.uid = value
    } else if key = "mobild_phone" {
        self.mobilePhone = value
    } else {
        print("undefine key:\(key), value:\(value)")
    }
}

递归问题

很多时候value不是一个基本类型, 还可以是对象和array

{
    "name" : "mike",
    "age" : 26
    "id" : 3344,
    "contact" : (
        "mobilePhone": "18688888888",
        "telPhone" : "010-00000000",
    )
    "address" : (
        {
            "city" : "Beijin",
            "code" : "100000".
            "uid" : 1,
        },
        {
            "city" : "Wuhan",
            "code" : "430000".
            "uid" : 2,
        },
    )
}

需要建model

public class ContactModel: NSObject {
    public var mobilePhone:String?
    public var telPhone:String?
}
public class AddressModel: NSObject {
    public var city:String?
    public var code:Int?
    public var uid:Int?
}

MemberModel如下

public class MemberModel: NSObject {
    public var name = ""
    public var age:Int?
    public var address:String?
    public var uid = 0
    public var contact: ContactModel?
    public var addressModels: [AddressModel]?

    func setValue(value: AnyObject!, forUndefinedKey key: String) {
        if key = "id" {
            self.uid = value
        } else if key = "contact" {
            self.contact = ContactModel(JSONDict: value)
        } else if key = "address" {
            self.address.removeAllObject()
            self.address = @[]
            for addressObject in value as NSArray {
                self.address.addObject(AddressModel(JSONDict: addressObject))
            }
        } else {
            print("undefine key:\(key), value:\(value)")
        } 
    }
}

性能提升

if else if是个On的过程, 当特殊处理的key较多时耗费时间比较长, 使用switch case会比较合适,但是当量比较大,内存紧张时,也不宜过多使用
仔细分析, 当需要特熟处理的key数量为3时,if elseswitch case耗费的平均跳转时间是一样的,当超过3时, switch case 能充分利用内存节省跳转时间, 以上函数可优化如下

 func setValue(value: AnyObject!, forUndefinedKey key: String) {
     switch key: {
         case "id": {
              self.uid = value
         }
         case "contact": {
             self.contact = ContactModel(JSONDict: value)
         }
         case "address": {
             self.address.removeAllObject()
             self.address = @[]
             for addressObject in value as NSArray {
                 self.address.addObject(AddressModel(JSONDict: addressObject))
             }
         }
         defualt: {
             print("undefine key:\(key), value:\(value)")
         }
     }
}

results matching ""

    No results matching ""