MJExtension 使用OC和Swift

作者: Pishum | 来源:发表于2018-01-02 15:53 被阅读123次

    1.普通json转model方式

    解析下面的一段json
    { "id":"324566", "name":"周瑜", "nick_name":"公瑾", "girl_friend":"小乔" }

    1.1 OC-Model解析

    建立OC类

    @interface OCPerson : NSObject
    
    @property (strong, nonatomic) NSString *personID;
    @property (strong, nonatomic) NSString *name;
    @property (strong, nonatomic) NSString *nickName;
    @property (strong, nonatomic) NSString *girlFriend;
    
    @end
    
    @implementation OCPerson
    
    
    + (NSDictionary *)mj_replacedKeyFromPropertyName{
        return @{@"personID" : @"id",
                 @"nickName": @"nick_name",
                 @"girlFriend": @"girl_friend"};
    }
    
    @end
    

    使用的时候

    OCPerson *person = [OCPerson mj_objectWithKeyValues:[self getPersonJson]];
    

    打断点如下图所示


    image.png

    1.2 Swift-Model解析

    建立Swift Model

    class SwiftPerson: NSObject {
        var personID: String?
        var name: String?
        var nickName: String?
        var girlFriend: String?
        
        override init() {
            super.init()
        }
        
        override static func mj_replacedKeyFromPropertyName() -> [AnyHashable : Any]{
            return ["personID": "id",
                    "nickName": "nick_name",
                    "girlFriend": "girl_friend"]
        }
    }
    

    在oc里使用

        SwiftPerson *person = [SwiftPerson mj_objectWithKeyValues:[self getPersonJson]];
        
        NSLog(@"person=%@",person.girlFriend);
    

    在Swift里使用

    let path = Bundle.main.path(forResource: "person.json", ofType: nil)
            
            
            do {
                guard let data = NSData.init(contentsOfFile: path!) else{
                    return
                }
                guard let dict = try JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableLeaves) as? Dictionary<AnyHashable, Any> else{
                    return
                }
                
                NSLog("dict=%@", dict)
                
                let person = SwiftPerson.mj_object(withKeyValues:dict)
                NSLog("person.girlFriend=%@", person?.girlFriend ?? "null")
                
            } catch  {
                
            }
    

    2.解析嵌套json Array

    需要解析如下的json

    {
        "start_date":"2017-12-12",
        "end_date":"2018-02-02",
        "items":[
            {
                "id":"324566",
                "name":"周瑜",
                "nick_name":"公瑾",
                "girl_friend":"小乔"
            },
            {
                "id":"432145",
                "name":"曹操",
                "nick_name":"孟德",
                "girl_friend":"抢人家小乔"
            }
        ]
    }
    

    2.1 OC Model解析

    2.1.1 建立OC People类

    @interface OCPeople : NSObject
    @property (strong, nonatomic) NSString *startDate;
    @property (strong, nonatomic) NSString *endDate;
    @property (strong, nonatomic) NSArray *list;
    @end
    

    2.1.2 配置key对应关系和array对应关系类

    @implementation OCPeople
    + (NSDictionary *)mj_replacedKeyFromPropertyName{
        return @{@"startDate" : @"start_date",
                 @"endDate": @"end_date",
                 @"list": @"items"};
    }
    
    + (NSDictionary *)mj_objectClassInArray{
        return @{
                 @"list" : @"OCPerson"
                 };
    }
    

    解析效果,打断点如下所示


    image.png

    2.2 Swift Model解析

    2.2.1 建立Swift People类

    class SwiftPeople: NSObject {
        
        var startDate: String?
        var endDate: String?
        var list: [SwiftPerson]?
        
        override init() {
            super.init()
        }
    }
    
    

    2.2.2 配置key关系和Array类关系

    override static func mj_replacedKeyFromPropertyName() -> [AnyHashable : Any]{
            return ["startDate": "start_date",
                    "endDate": "end_date",
                    "list": "items"]
        }
        
        override func mj_keyValuesDidFinishConvertingToObject() {
            if self.list != nil {
                
                let items = SwiftPerson.mj_objectArray(withKeyValuesArray: self.list)
                if items != nil {
                    self.list = items! as NSArray as? [SwiftPerson]
                }
            }
        }
    

    在这里不能像OC那样重写mj_objectClassInArray方法,而是需要重写mj_keyValuesDidFinishConvertingToObject方法(具体原因可以看源码,下回分解)
    在OC里调用解析效果如下:

    image.png

    在Swift里调用解析效果如下:


    image.png

    点击源码

    相关文章

      网友评论

        本文标题:MJExtension 使用OC和Swift

        本文链接:https://www.haomeiwen.com/subject/rwefnxtx.html