ios MJExtension 数据解析转换

一、MJExtension第三方框架

      我们在iOS开发过程中,我们常常需要将字典数据(也就是JSON数据)与Model模型之间的转化,例如网络请求返回的微博数据、等等,如果我们自己全部手动去创建模型并赋值,都是一些毫无技术含量的代码,费时费力,而且还可能会赋值出错,让我们很头疼。MJExtension框架就是为了解决这个问题而设计得第三方开源库。

MJExtension的github地址点这里:https://github.com/CoderMJLee/MJExtension

 

  二、MJExtension使用     

          1. 简单的字典 --> 模型

          2. JSON字符串 --> 模型

          3. 复杂的字典 --> 模型 (模型里面包含了模型)

          4. 复杂的字典 --> 模型 (模型的数组属性里面又装着模型)

          5. 复杂的字典 --> 模型(模型属性名和字典的key不一样)

          6. 字典数组 --> 模型数组

          7. 模型 --> 字典

          8. 模型数组 --> 字典数组

          9. 字典 --> CoreData模型

          10. 归档与解档NSCoding

          11. 过滤字典的值

 

    1. 简单的字典 --> 模型

          模型类User定义:      

typedef enum {
    SexMale,
    SexFemale
} Sex;
@interface User : NSObject
@property (copy, nonatomic) NSString *name;/* 姓名 */
@property (copy, nonatomic) NSString *icon;/* 头像 */
@property (assign, nonatomic) unsigned int age;/* 年龄 */
@property (copy, nonatomic) NSString *height;/* 身高 */
@property (strong, nonatomic) NSNumber *money;/* 资产 */
@property (assign, nonatomic) Sex sex;/* 性别 */
@property (assign, nonatomic, getter=isGay) BOOL gay;/* 是否是同性恋 */
@end

          使用实例:

NSDictionary *dict = @{
    @"name" : @"Jack",
    @"icon" : @"lufy.png",
    @"age" : @20,
    @"height" : @"1.55",
    @"money" : @100.9,
    @"sex" : @(SexFemale),/* 枚举需要使用NSNumber包装 */
    @"gay" : @"NO"
};
//字典转模型,使用的是mj_objectWithKeyValues:方法
User *user = [User mj_objectWithKeyValues:dict];

 

    2. JSON字符串 --> 模型

        使用实例:

// 定义一个JSON字符串
NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
// JSON字符串转模型
User *user = [User mj_objectWithKeyValues:jsonString];
 

    3. 复杂的字典 --> 模型 (模型里面包含了模型)

        模型类Status定义:

@interface Status : NSObject
@property (copy, nonatomic) NSString *text;
@property (strong, nonatomic) User *user;/* 其他模型类型 */
@property (strong, nonatomic) Status *retweetedStatus;/* 自我模型类型 */
@end

 

        使用实例:

NSDictionary *dict = @{
    @"text" : @"Agree!Nice weather!",
    @"user" : @{
        @"name" : @"Jack",
        @"icon" : @"lufy.png"
    },
    @"retweetedStatus" : @{
        @"text" : @"Nice weather!",
        @"user" : @{
            @"name" : @"Rose",
            @"icon" : @"nami.png"
        }
    }
};
//字典转模型,模型里面含有模型
Status *status = [Status mj_objectWithKeyValues:dict];

NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
// text=Agree!Nice weather!, name=Jack, icon=lufy.png
NSString *text2 = status.retweetedStatus.text; NSString *name2 = status.retweetedStatus.user.name; NSString *icon2 = status.retweetedStatus.user.icon; NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2); // text2=Nice weather!, name2=Rose, icon2=nami.png

 

4. 复杂的字典 --> 模型 (模型的数组属性里面又装着模型)

    模型类Ad和StatusResult定义:

@interface Ad : NSObject
@property (copy, nonatomic) NSString *image;
@property (copy, nonatomic) NSString *url;
@end

@interface StatusResult : NSObject
/** 数组中存储模型Status类型数据 */
@property (strong, nonatomic) NSMutableArray *statuses;
/** 数组中存储模型Ad类型数据 */
@property (strong, nonatomic) NSArray *ads;
@property (strong, nonatomic) NSNumber *totalNumber;
@end

#import "MJExtension.h"
/* 数组中存储模型数据,需要说明数组中存储的模型数据类型 */
@implementation StatusResult
/* 实现该方法,说明数组中存储的模型数据类型 */
+ (NSDictionary *)mj_ objectClassInArray{
    return @{ @"statuses" : @"Status",
              @"ads" : @"Ad"
            };
}
@end

 

        使用实例:

NSDictionary *dict = @{
    @"statuses" : @[
                      @{
                          @"text" : @"Nice weather!",
                          @"user" : @{
                              @"name" : @"Rose",
                              @"icon" : @"nami.png"
                          }
                      },
                      @{
                          @"text" : @"Go camping tomorrow!",
                          @"user" : @{
                              @"name" : @"Jack",
                              @"icon" : @"lufy.png"
                          }
                      }
                  ],
    @"ads" : @[
                 @{
                     @"image" : @"ad01.png",
                     @"url" : @"http://www.ad01.com"
                 },
                 @{
                     @"image" : @"ad02.png",
                     @"url" : @"http://www.ad02.com"
                 }
             ],
    @"totalNumber" : @"2014"
};
//字典转模型,支持模型的数组属性里面又装着模型
StatusResult *result = [StatusResult mj_objectWithKeyValues:dict];
//打印博主信息
for (Status *status in result.statuses) {
    NSString *text = status.text;
    NSString *name = status.user.name;
    NSString *icon = status.user.icon;
    NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
}
// text=Nice weather!, name=Rose, icon=nami.png
// text=Go camping tomorrow!, name=Jack, icon=lufy.png
//打印广告
for (Ad *ad in result.ads) {
    NSLog(@"image=%@, url=%@", ad.image, ad.url);
}
// image=ad01.png, url=http://www.ad01.com
// image=ad02.png, url=http://www.ad02.com

 

5. 复杂的字典 --> 模型(模型属性名和字典的key不一样)

     模型类Bag和Student定义:

@interface Bag : NSObject
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end

@interface Student : NSObject
@property (copy, nonatomic) NSString *ID;
@property (copy, nonatomic) NSString *desc;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *oldName;
@property (copy, nonatomic) NSString *nameChangedTime;
@property (strong, nonatomic) Bag *bag;
@end

#import "MJExtension.h"
@implementation 
/* 设置模型属性名和字典key之间的映射关系 */
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    /* 返回的字典,key为模型属性名,value为转化的字典的多级key */
    return @{
               @"ID" : @"id",
               @"desc" : @"desciption",
               @"oldName" : @"name.oldName",
               @"nowName" : @"name.newName",
               @"nameChangedTime" : @"name.info[1].nameChangedTime",
               @"bag" : @"other.bag"
           };
}
@end 
     
         使用实例:
NSDictionary *dict = @{
    @"id" : @"20",
    @"desciption" : @"kids",
    @"name" : @{
        @"newName" : @"lufy",
        @"oldName" : @"kitty",
        @"info" : @[
                 @"test-data",
                 @{
                             @"nameChangedTime" : @"2013-08"
                         }
                  ]
    },
    @"other" : @{
        @"bag" : @{
            @"name" : @"a red bag",
            @"price" : @100.7
        }
    }
};
//字典转模型,支持多级映射
Student *stu = [Student mj_objectWithKeyValues:dict];
//打印
NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
      stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
// ID=20, desc=kids, oldName=kitty, nowName=lufy, nameChangedTime=2013-08
NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
// bagName=a red bag, bagPrice=100.700000

 

   6. 字典数组 --> 模型数组

        使用实例:

NSArray *dictArray = @[
                         @{
                             @"name" : @"Jack",
                             @"icon" : @"lufy.png"
                         },
                         @{
                             @"name" : @"Rose",
                             @"icon" : @"nami.png"
                         }
                     ];
//字典数组转模型数组,使用的是mj_objectArrayWithKeyValuesArray:方法
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
//打印
for (User *user in userArray) {
    NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
// name=Jack, icon=lufy.png
// name=Rose, icon=nami.png

 

   7. 模型 --> 字典

    使用实例:

//创建一个模型对象
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
Status *status = [[Status alloc] init];
status.user = user;
status.text = @"Nice mood!";
//模型转字典,使用的是mj_keyValues属性
NSDictionary *statusDict = status.mj_keyValues;
NSLog(@"%@", statusDict);
/*
 {
 text = "Nice mood!";
 user =     {
 icon = "lufy.png";
 name = Jack;
 };
 }
 */

 

   8. 模型数组 --> 字典数组

        使用实例:

//创建模型数组
User *user1 = [[User alloc] init];
user1.name = @"Jack";
user1.icon = @"lufy.png";
User *user2 = [[User alloc] init];
user2.name = @"Rose";
user2.icon = @"nami.png";
NSArray *userArray = @[user1, user2];
//模型数组转字典数组,使用的是mj_keyValuesArrayWithObjectArray:方法
NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
NSLog(@"%@", dictArray);
/*
 (
 {
 icon = "lufy.png";
 name = Jack;
 },
 {
 icon = "nami.png";
 name = Rose;
 }
 )
 */

 

   9. 字典 --> CoreData模型

        使用实例:

NSDictionary *dict = @{
                         @"name" : @"Jack",
                         @"icon" : @"lufy.png",
                         @"age" : @20,
                         @"height" : @1.55,
                         @"money" : @"100.9",
                         @"sex" : @(SexFemale),
                         @"gay" : @"true"
                     };
//字典转为CoreData模型
NSManagedObjectContext *context = nil;
User *user = [User mj_objectWithKeyValues:dict 
                                  context:context];
[context save:nil];

 

10. 归档与解档NSCoding

      模型类Bag添加实现:

@interface Bag : NSObject <NSCoding>
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end

#import "MJExtension.h"
@implementation Bag
//添加了下面的宏定义
MJExtensionCodingImplementation

/* 实现下面的方法,说明哪些属性不需要归档和解档 */
+ (NSArray *)mj_ignoredCodingPropertyNames{
    return @[@"name"];
}
@end

 

       使用实例:

//创建模型
Bag *bag = [[Bag alloc] init];
bag.name = @"Red bag";
bag.price = 200.8;
//获取归档路径
NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/bag.data"];
//归档
[NSKeyedArchiver archiveRootObject:bag toFile:file];
//解档
Bag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
NSLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price);
// name=(null), price=200.800000

 

11. 过滤字典的值

     模型类Book实现:

@interface Book: NSObject
@property (copy, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate *publishedTime;
@end

#import "MJExtension.h"
@implementation Book
/* 转化过程中对字典的值进行过滤和进一步转化 */
- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property
{
    if ([property.name isEqualToString:@"publisher"]) {
        if (oldValue == nil) {
            return @"";
        }        
    } else if (property.type.typeClass == [NSDate class]) {
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd";
        return [fmt dateFromString:oldValue];
    }
    return oldValue;
}
@end

 

          使用实例:

NSDictionary *dict = @{
                       @"name" : @"5分钟突破iOS开发",
                       @"publishedTime" : @"2011-09-10"
                       };
//字典转模型,过滤name为nil的情况,把NSString转为NSDate
Book *book = [Book mj_objectWithKeyValues:dict];
//打印
NSLog(@"name=%@, publishedTime=%@", book.name, book.publishedTime);

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2022-01-11 17:21  张亚楠  阅读(818)  评论(0编辑  收藏  举报