MJExtension是一套常用的"字典和模型之间互相转换"的框架,在项目中也使用过,现在记录一下。随着Swift的普及,在Swift中也有一个类似功能的框架HandyJSON 也非常好用。有空我也会介绍一下这个框架。

MJExtension 能完成的功能

<1> 字典转模型

<2>模型转字典

<3>字典数组->模型数组

<4>模型数组->字典数组

一 字典转模型

//字典转模型
- (void) dicToModel {
//简单的字典
NSDictionary *dict_user = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @,
@"height" : @"1.55",
@"money" : @100.9,
@"sex" : @(SexFemale),/* 枚举需要使用NSNumber包装 */
@"gay" : @YES
};
User *user = [User mj_objectWithKeyValues:dict_user];
NSLog(@"%@ %u %@",user.name,user.sex,user.icon); }

二 JSON字符串转模型

- (void) stringToModel {
NSString *jsonStr = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
User *user = [User mj_objectWithKeyValues:jsonStr];
NSLog(@"%@ %u %@",user.name,user.age,user.icon);
}

三 复杂的字典转模型

@interface Status : NSObject

@property (nonatomic,copy) NSString *text;
@property (nonatomic,strong) User *user;
@property (nonatomic,strong) Status *retweetedStatus; @end - (void)complexDicToModel {
NSDictionary *dict_m8m = @{
@"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_m8m];
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"mj-----text=%@, name=%@, icon=%@", text, name, icon);
NSString *text2 = status.retweetedStatus.text;
NSString *name2 = status.retweetedStatus.user.name;
NSString *icon2 = status.retweetedStatus.user.icon;
NSLog(@"mj-----text2=%@, name2=%@, icon2=%@", text2, name2, icon2); }

四 模型中有个数组属性,数组里面又装着其他属性

@interface Status : NSObject

@property (nonatomic,copy) NSString *text;
@property (nonatomic,strong) User *user;
@property (nonatomic,strong) Status *retweetedStatus; @end @interface ADModel : NSObject @property (nonatomic,copy) NSString *image; @property (nonatomic,copy) NSString *url; @end @interface resultModel : NSObject @property (nonatomic,strong) NSMutableArray *statuses;
@property (nonatomic,strong) NSMutableArray *ads;
@property (nonatomic,strong) NSNumber *totalNumber;
@property (nonatomic,assign) long long previousCursor;
@property (nonatomic,assign) long long nextCursor; @end @implementation resultModel + (NSDictionary *)mj_objectClassInArray {
return @{@"statuses" : @"Status", @"ads":@"ADModel"};
} @end - (void)complexDicContentArrToModel {
// 1.定义一个字典
NSDictionary *dict = @{
@"statuses" : @[
@{
@"text" : @"今天天气真不错!", @"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}, @{
@"text" : @"明天去旅游了", @"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
}
} ], @"ads" : @[
@{
@"image" : @"ad01.png",
@"url" : @"http://www.小码哥ad01.com"
},
@{
@"image" : @"ad02.png",
@"url" : @"http://www.小码哥ad02.com"
}
], @"totalNumber" : @"",
@"previousCursor" : @"",
@"nextCursor" : @""
};
resultModel *model = [resultModel mj_objectWithKeyValues:dict];
NSLog(@"resultModel %lld ",model.nextCursor,model.previousCursor);
// 4.打印statuses数组中的模型属性
for (Status *status in model.statuses) {
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
MJExtensionLog(@"text=%@, name=%@, icon=%@", text, name, icon);
} // 5.打印ads数组中的模型属性
for (ADModel *ad in model.ads) {
MJExtensionLog(@"image=%@, url=%@", ad.image, ad.url);
} }

五 简单的字典转模型 (key替换 比如ID和id,支持多级映射)

@interface Bag : NSObject

@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) CGFloat price; @end @interface Student : NSObject @property (copy, nonatomic) NSString *ID;
@property (copy, nonatomic) NSString *otherName;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *oldName;
@property (copy, nonatomic) NSString *nameChangedTime;
@property (copy, nonatomic) NSString *desc;
@property (strong, nonatomic) Bag *bag; @end @implementation Student + (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{@"ID":@"id",@"desc":@"desciption",@"oldName":@"name.oldName",@"nowName":@"name.newName",@"nameChangedTime":@"name.info[1].nameChangedTime",@"bag":@"other.bag"};
} @end - (void)keyValues2object4 {
// 1.定义一个字典
NSDictionary *dict = @{
@"id" : @"",
@"desciption" : @"好孩子",
@"name" : @{
@"newName" : @"lufy",
@"oldName" : @"kitty",
@"info" : @[
@"test-data",
@{@"nameChangedTime" : @"2013-08-07"}
]
},
@"other" : @{
@"bag" : @{
@"name" : @"小书包",
@"price" : @100.7
}
}
}; // 2.将字典转为MJStudent模型
Student *stu = [Student mj_objectWithKeyValues:dict]; // 3.打印MJStudent模型的属性
MJExtensionLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@", stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
MJExtensionLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
}

六 将一个字典数组转成模型数组

- (void)arrayToModel {
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
];
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
}

七 将一个模型转成字典

- (void)modelToDict {
User *user = [[User alloc] init];
user.name = @"jack";
user.icon = @"lufy.png"; NSDictionary *userDic = user.mj_keyValues;
NSLog(@"%@",userDic);
}

八 将一个模型数组转成字典数组

//模型数组 转 字典数组
- (void)modelArrayToDicArray {
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]; NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray]; }

九 NSCoding 示例

#import <Foundation/Foundation.h>
#import "MJExtension.h" @interface LFCar : NSObject @property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) double price; @end #import "LFCar.h" @implementation LFCar MJExtensionCodingImplementation @end - (void)viewDidLoad {
[super viewDidLoad]; LFCar *car = [[LFCar alloc] init];
car.name = @"red bag";
car.price = 200.8; NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"bag.data"];
//归档
[NSKeyedArchiver archiveRootObject:car toFile:file];
//解档
LFCar *decodedCar = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
NSLog(@"%@ %f",decodedCar.name,decodedCar.price);
}

这就是MJExtension 的基本用法 还有一些其他的不常用的用法 参考这里http://blog.csdn.net/jeikerxiao/article/details/51590222

最新文章

  1. Xamarin.Android之Splash的几种简单实现
  2. 将.dat文件导入数据库
  3. ASP.NET ZERO 学习 HangFire的使用二
  4. angularjs decorator
  5. 蓝牙4.0 BLE基础之vdd检测new
  6. 关于view.measure
  7. php 共享内存
  8. Netsharp快速入门(之15) 销售管理(报表B 销售季度表)
  9. CGI 是什么
  10. C#_delegate - 异步调用实例 BeginInvoke EndInvoke event
  11. Common-logging 与 Log4j的结合使用
  12. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(五)
  13. JS 昵称,手机号,邮箱判断
  14. win 结束占用端口的进程
  15. Java设计模式学习笔记,一:单例模式
  16. Jupyter Notebook使用小技巧
  17. Linux显示更新十次后退出
  18. VS2005工程的Device右边内容为空问题
  19. .Net Discovery系列之十-深入理解平台机制与性能影响(上)
  20. 总结ASP.NET MVC视图页使用jQuery传递异步数据的几种方式

热门文章

  1. 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求
  2. JAVA Eclipse如何修改Android程序名称
  3. WordPress安全检测工具
  4. Android错误之--Error retrieving parent for item: No resource found that matches the given name &amp;#39;Theme.A
  5. HTML &lt;!DOCTYPE&gt; (转自w3school)
  6. servlet监听器与事件
  7. MVC基础操作
  8. Oracle Golden Gate基本配置
  9. unity开发android游戏(一)搭建Unity安卓开发环境
  10. Exploiting CVE-2015-2509 /MS15-100 : Windows Media Center could allow remote code execution