如果项目是纯OC的建议使用,MJExtension是一套字典和模型之间互相转换的超轻量级框架,可以轻松完成:

 字典(JSON) --> 模型(Model)
模型(Model) --> 字典(JSON)
字典数组(JSON Array) --> 模型数组(Model Array)
模型数组(Model Array) --> 字典数组(JSON Array)
如果是swift的项目推荐使用SwiftyJSON,HandyJSON。当然还有其他的解析方式在这里就不说了。如果项目混编我还是喜欢MJExtension。下面说方法:

1.最简单的字典转模型

typedef enum {
SexMale,
SexFemale} Sex;
@interface User : NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *icon;
@property (assign, nonatomic) int age;
@property (assign, nonatomic) double height;
@property (strong, nonatomic) NSNumber *money;
@property (assign, nonatomic) Sex sex;
@end NSDictionary *dict = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @20,
@"height" : @"1.55",
@"money" : @100.9,
@"sex" : @(SexFemale)
}; // 将字典转为User模型
User *user = [User objectWithKeyValues:dict];
NSLog(@"name=%@, icon=%@, age=%d, height=%@, money=%@, sex=%d", user.name, user.icon, user.age, user.height, user.money, user.sex);
// name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1

核心代码1:

[User objectWithKeyValues:dict]

2.模型中嵌套模型

@interface Status : NSObject
/** 微博文本内容 */
@property (copy, nonatomic) NSString *text;
/** 微博作者 */
@property (strong, nonatomic) User *user;
/** 转发的微博 */
@property (strong, nonatomic) Status *retweetedStatus;
@end NSDictionary *dict = @{
@"text" : @"是啊,今天天气确实不错!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@"retweetedStatus" : @{
@"text" : @"今天天气真不错!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}
}; // 将字典转为Status模型
Status *status = [Status objectWithKeyValues:dict];
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
// text=是啊,今天天气确实不错!, 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=今天天气真不错!, name2=Rose, icon2=nami.png

核心代码2

[Status objectWithKeyValues:dict]

3.模型中有个数组属性,数组里面又要装着其它模型

@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 @implementation StatusResult
// 实现这个方法的目的:告诉MJExtension框架statuses和ads数组里面装的是什么模型
/* + (NSDictionary *)objectClassInArray{
return @{
@"statuses" : [Status class],
@"ads" : [Ad class] };
}
+ (Class)objectClassInArray:(NSString *)propertyName{
if ([propertyName isEqualToString:@"statuses"]) {
return [Status class];
} else if ([propertyName isEqualToString:@"ads"]) {
return [Ad class]; }
return nil;}
*/ // 这个方法对比上面的2个方法更加没有侵入性和污染,因为不需要导入Status和Ad的头文件
+ (NSDictionary *)objectClassInArray{
return @{
@"statuses" : @"Status",
@"ads" : @"Ad"
};
}
@end 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" : @"2014"
}; // 将字典转为StatusResult模型
StatusResult *result = [StatusResult objectWithKeyValues:dict];
NSLog(@"totalNumber=%@", result.totalNumber);
// totalNumber=2014 // 打印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);}
// text=今天天气真不错!, name=Rose, icon=nami.png
// text=明天去旅游了, name=Jack, icon=lufy.png // 打印ads数组中的模型属性
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

核心代码3:

在模型内部实现+ (NSDictionary *)objectClassInArray方法

[StatusResult objectWithKeyValues:dict]

4.模型中的属性名和字典中的key不相同(或者需要多级映射)

@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 @implementation Student
// 实现这个方法的目的:告诉MJExtension框架模型中的属性名对应着字典的哪个key
+ (NSDictionary *)replacedKeyFromPropertyName{
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info.nameChangedTime",
@"bag" : @"other.bag"
};
} @end NSDictionary *dict = @{
@"id" : @"20",
@"desciption" : @"孩子",
@"name" : @{
@"newName" : @"lufy",
@"oldName" : @"kitty",
@"info" : @{
@"nameChangedTime" : @"2013-08"
}
},
@"other" : @{
@"bag" : @{
@"name" : @"小书包",
@"price" : @100.7
}
}
}; // 将字典转为Student模型
Student *stu = [Student objectWithKeyValues:dict];
// 打印Student模型的属性
NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@", stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
// ID=20, desc=孩子, oldName=kitty, nowName=lufy, nameChangedTime=2013-08
NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
// bagName=小书包, bagPrice=100.700000

核心代码4:

在模型内部实现+ (NSDictionary *)replacedKeyFromPropertyName方法

[Student objectWithKeyValues:dict]

5.将一个字典数组转成模型数组

NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png",
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png",
}
]; // 将字典数组转为User模型数组
NSArray *userArray = [User objectArrayWithKeyValuesArray:dictArray];
// 打印userArray数组中的User模型属性
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);}
// name=Jack, icon=lufy.png
// name=Rose, icon=nami.png

核心代码5:

[User objectArrayWithKeyValuesArray:dictArray]

6.将一个模型转成字典

// 新建模型
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png"; Status *status = [[Status alloc] init];
status.user = user;
status.text = @"今天的心情不错!"; // 将模型转为字典
NSDictionary *statusDict = status.keyValues;
NSLog(@"%@", statusDict);
/*{ text = "今天的心情不错!";
user = {
icon = "lufy.png";
name = Jack;
};
}*/ // 多级映射的模型
Student *stu = [[Student alloc] init];
stu.ID = @"123";
stu.oldName = @"rose";
stu.nowName = @"jack";
stu.desc = @"handsome";
stu.nameChangedTime = @"2018-09-08"; Bag *bag = [[Bag alloc] init];
bag.name = @"小书包";
bag.price = 205;
stu.bag = bag;
NSDictionary *stuDict = stu.keyValues;NSLog(@"%@", stuDict);
/*
{ desciption = handsome;
id = 123;
name = {
info ={
nameChangedTime = "2018-09-08";
};
newName = jack;
oldName = rose;
};
other = {
bag ={
name = "小书包";
price = 205;
};
};
}
*/

核心代码6:

status.keyValues、stu.keyValues

7.将一个模型数组转成字典数组

// 新建模型数组
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 keyValuesArrayWithObjectArray:userArray];
NSLog(@"%@", dictArray);
/*(
{ icon = "lufy.png"; name = Jack; },
{ icon = "nami.png"; name = Rose; } )*/

核心代码7:

[User keyValuesArrayWithObjectArray:userArray];
 

iOS字典转模型MJExtension使用的更多相关文章

  1. iOS 字典转模型Model

    基本原理 利用 runtime 原理,获取模型中所有实例变量列表,根据实例变量以此获取模型中成员变量的名称和属性类型,区分Foundation和自定义属性,需要对NSDictionary和NSArra ...

  2. ios 字典转模型

    <pre name="code" class="objc">#import <Foundation/Foundation.h> @int ...

  3. iOS开发——网络篇——JSON和XML,NSJSONSerialization ,NSXMLParser(XML解析器),NSXMLParserDelegate,MJExtension (字典转模型),GDataXML(三方框架解析XML)

    一.JSON 1.JSON简介什么是JSONJSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典 ...

  4. IOS 字典模型互转框架 MJExtension

    IOS 字典模型互转框架 MJExtension   能做什么? MJExtension是一套字典和模型之间互相转换的超轻量级框架 MJExtension能完成的功能 字典(JSON) -->  ...

  5. iOS开发——笔记篇&关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结

    关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结 一:Plist读取 /************************************ ...

  6. ios开发网络学习二:URL转码以及字典转模型框架MJExtension的使用

    一:url转码,当url中涉及到中文的时候,要考虑转码,用UTF8对中文的url进行转码 #import "ViewController.h" @interface ViewCon ...

  7. IOS第四天-新浪微博 -存储优化OAuth授权账号信息,下拉刷新,字典转模型

    *************application - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...

  8. 字典转模型框架 Mantle的使用:国外程序员最常用的iOS模型

    Mantle简介 Mantle 是iOS和Mac平台下基于Objective-C编写的一个简单高效的模型层框架. Mantle能做什么 Mantle可以轻松把JSON数据.字典(Dictionary) ...

  9. iOS开发——高级技术精选OC篇&Runtime之字典转模型实战

    Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...

随机推荐

  1. js的双等号类型转换

    var undefined; undefined == null; // true 1 == true; // true 2 == true; // false 0 == false; // true ...

  2. web前端面试总结(二)

    这段时间大大小小面试确实不少,相对之前那篇被虐到体无完肤这几次确实相对来说有很大进步这里总结一下: 1.发现自己,站在个人角度我还是挺赞成出去面试的,不管你对现在的公司是否满意,当你觉得在这里已经有一 ...

  3. MySQL,Oracle,PostgreSQL 数据库web维护客户端管理工具

    TreeDMS数据库管理系统使用JAVA开发,采用稳定通用的springMVC +JDBC架构,实现基于WEB方式对 MySQL,Oracle,PostgreSQL 等数据库进行维护管理操作. 功能包 ...

  4. Vue 项目实战系列 (三)

    我们继续前两节的开发.本节教程实现的效果如下: 效果很简单,但是实现起来却要用到Vue的很多知识,下面我们将一步一步的实现这个效果. 首先这些城市的信息都是从后台的server里面获取的,所以我们需要 ...

  5. sql with as 用法(转载)

    一.WITH AS的含义     WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...

  6. Mac, Linux中配置Latex中文字体

    对于中文的latex文档,在Linux下一般可以使用系统自带的开源字体:文泉驿(WenQuanYi)来实现,即如下的最小例子,通过xelatex命令来编译即可生成中文文档. \documentclas ...

  7. sql hibernate查询转换成实体或对应的VO Transformers

    sql查询转换成实体或对应的VO Transformers //addScalar("id") 默认查询出来的id是全部大写的(sql起别名也无效,所以使用.addScalar(& ...

  8. 【LeetCode】67. Add Binary

    题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...

  9. 分页插件Jpages的使用

    项目原因需要前端做分页表格,之前做了一个ul的分页效果,但是感觉自己写还是造轮子了,今天网上看到Jpqges插件就试了下,感觉平时使用挺方便的,写一下自己的使用过程. 先上套图,下载下来就2个js和1 ...

  10. 关于引入多个jquery冲突的问题(附一个很好用的validate前端验证框架及使用方法)

    废话不多说,进入正题: 如果一个jsp中想要使用两个不同版本的jquery怎么办呢?客官往下看: <script src="${ctxStatic}/jquery/jquery-1.8 ...