框架下载地址是:
https://github.com/makeLaugh/MJExtension
这边主要的一个示例是将一个从web上面取到的json字符窜转化成model的一个流程,开始集成吧。
1)下载解压
![IOS MJExtension json转模型的轻量级框架的使用_第1张图片](http://img.e-com-net.com/image/product/61a9e3c8b7d54c6bb9341b494e5dbe42.jpg)
2)添加到新的项目中
3)导入头文件后,开始使用这个文件,这边是从web上面取得的一个数据,直接映射成一个model对象
这里只是其中的一个使用方法,更多的方法可以查看其下载文件中的main方便中的更加详细的调用 。
//
//
main.m
//
字典与模型的互转
//
//
Created by MJ Lee on 14-5-21.
//
Copyright (c) 2014年 itcast. All rights reserved.
//
/*
*
MJ友情提醒:
1.MJExtension是一套“字典和模型之间互相转换”的轻量级框架
2.MJExtension能完成的功能
* 字典 --> 模型
* 模型 --> 字典
* 字典数组 --> 模型数组
* 模型数组 --> 字典数组
3.具体用法主要参考 main.m中各个函数 以及 "NSObject+MJKeyValue.h"
4.希望各位大神能用得爽
*/
#import <Foundation/Foundation.h>
#import
"
MJExtension.h
"
#import
"
User.h
"
#import
"
Status.h
"
#import
"
StatusResult.h
"
/*
*
* 简单的字典 -> 模型
*/
void keyValues2object()
{
//
1.定义一个字典
NSDictionary *dict = @{
@"
name
" :
@"
Jack
",
@"
icon
" :
@"
lufy.png
",
};
//
2.将字典转为User模型
User *user = [User objectWithKeyValues:
@"
44
"];
//
3.打印User模型的属性
NSLog(
@"
name=%@, icon=%@
", user.name, user.icon);
}
/*
*
* 复杂的字典 -> 模型 (模型里面包含了模型)
*/
void keyValues2object2()
{
//
1.定义一个字典
NSDictionary *dict = @{
@"
text
" :
@"
是啊,今天天气确实不错!
",
@"
user
" : @{
@"
name
" :
@"
Jack
",
@"
icon
" :
@"
lufy.png
"
},
@"
retweetedStatus
" : @{
@"
text
" :
@"
今天天气真不错!
",
@"
user
" : @{
@"
name
" :
@"
Rose
",
@"
icon
" :
@"
nami.png
"
}
}
};
//
2.将字典转为Status模型
Status *status = [Status objectWithKeyValues:dict];
//
3.打印status的属性
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(
@"
text=%@, name=%@, icon=%@
", text, name, icon);
//
4.打印status.retweetedStatus的属性
NSString *text2 = status.retweetedStatus.text;
NSString *name2 = status.retweetedStatus.user.name;
NSString *icon2 = status.retweetedStatus.user.icon;
NSLog(
@"
text2=%@, name2=%@, icon2=%@
", text2, name2, icon2);
}
/*
*
* 复杂的字典 -> 模型 (模型的数组属性里面又装着模型)
*/
void keyValues2object3()
{
//
1.定义一个字典
NSDictionary *dict = @{
@"
statuses
" : @[
@{
@"
text
" :
@"
今天天气真不错!
",
@"
user
" : @{
@"
name
" :
@"
Rose
",
@"
icon
" :
@"
nami.png
"
}
},
@{
@"
text
" :
@"
明天去旅游了
",
@"
user
" : @{
@"
name
" :
@"
Jack
",
@"
icon
" :
@"
lufy.png
"
}
},
@{
@"
text
" :
@"
嘿嘿,这东西不错哦!
",
@"
user
" : @{
@"
name
" :
@"
Jim
",
@"
icon
" :
@"
zero.png
"
}
}
],
@"
totalNumber
" :
@"
2014
",
@"
previousCursor
" :
@"
13476589
",
@"
nextCursor
" :
@"
13476599
"
};
//
2.将字典转为StatusResult模型
StatusResult *result = [StatusResult objectWithKeyValues:dict];
//
3.打印StatusResult模型的简单属性
NSLog(
@"
totalNumber=%d, previousCursor=%lld, nextCursor=%lld
", result.totalNumber, result.previousCursor, result.nextCursor);
//
4.打印statuses数组中的模型属性
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);
}
}
/*
*
* 字典数组 -> 模型数组
*/
void keyValuesArray2objectArray()
{
//
1.定义一个字典数组
NSArray *dictArray = @[
@{
@"
name
" :
@"
Jack
",
@"
icon
" :
@"
lufy.png
",
},
@{
@"
name
" :
@"
Rose
",
@"
icon
" :
@"
nami.png
",
},
@{
@"
name
" :
@"
Jim
",
@"
icon
" :
@"
zero.png
",
}
];
//
2.将字典数组转为User模型数组
NSArray *userArray = [User objectArrayWithKeyValuesArray:dictArray];
//
3.打印userArray数组中的User模型属性
for (User *user
in userArray) {
NSLog(
@"
name=%@, icon=%@
", user.name, user.icon);
}
}
/*
*
* 模型 -> 字典
*/
void object2keyValues()
{
//
1.新建模型
User *user = [[User alloc] init];
user.name =
@"
Jack
";
user.icon =
@"
lufy.png
";
Status *status = [[Status alloc] init];
status.user = user;
status.text =
@"
今天的心情不错!
";
//
2.将模型转为字典
//
NSDictionary *dict = [status keyValues];
NSDictionary *dict = status.keyValues;
NSLog(
@"
%@
", dict);
}
/*
*
* 模型数组 -> 字典数组
*/
void objectArray2keyValuesArray()
{
//
1.新建模型数组
User *user1 = [[User alloc] init];
user1.name =
@"
Jack
";
user1.icon =
@"
lufy.png
";
User *user2 = [[User alloc] init];
user2.name =
@"
Rose
";
user2.icon =
@"
nami.png
";
User *user3 = [[User alloc] init];
user3.name =
@"
Jim
";
user3.icon =
@"
zero.png
";
NSArray *userArray = @[user1, user2, user3];
//
2.将模型数组转为字典数组
NSArray *dictArray = [User keyValuesArrayWithObjectArray:userArray];
NSLog(
@"
%@
", dictArray);
}
int main(
int argc,
const
char * argv[])
{
@autoreleasepool {
//
简单的字典 -> 模型
//
keyValues2object();
//
复杂的字典 -> 模型 (模型里面包含了模型)
keyValues2object2();
//
复杂的字典 -> 模型 (模型的数组属性里面又装着模型)
//
keyValues2object3();
//
字典数组 -> 模型数组
//
keyValuesArray2objectArray();
//
模型转字典
//
object2keyValues();
//
模型数组 -> 字典数组
//
objectArray2keyValuesArray();
}
return
0;
}