iOS Json转换模型库:YYModel
来源:互联网 发布:文艺知乎 编辑:程序博客网 时间:2024/05/22 06:58
iOS Json转换模型库:YYModel
来自: 增删改查 地址:http://simayang.com/archives/620.html#comment-61
其实在研究这个库之前,市面上已经有很多类似的模型序列化成JSON及反序列化库(如Mantle、MJExtension)了,推荐他只是因为他高端的性能和容错(错误对象类型赋值到属性时YYMODEL会尝试自动转换,避免Crash)以及低侵入(不需要你的MODEL类去继承某个基类、因为他是Category 方式来实现的)。作者号称对比性能如下:
接下来直接写一个小例子看如何使用:
1.首先准备JSON及对象如下:
{ "userName": "向阳", "userPass": "xiang", "age": 10, "ident": [ { "price": 100.56, "priceDate": "1987-06-13 00:00:00" }, { "price": 100, "priceDate": "1987-06-13" } ]}
模型:Ident
@interface Ident : NSObject@property(nonatomic,strong) NSNumber* price;@property(nonatomic,strong) NSDate* priceDate;@end#import "Ident.h"@implementation Ident@end
模型:User (对象有包含关系时,在包含类的中需要申明一个modelContainerPropertyGenericClass方法,并标明对应属性以及转换的对象类。如这里的User包含了Ident)
#import <Foundation/Foundation.h>#import "Ident.h"@interface User : NSObject@property(nonatomic,strong)NSString* userName;@property(nonatomic,strong)NSString* userPass;@property(nonatomic,strong)NSNumber* age;@property(nonatomic,strong)NSArray<Ident*>* ident;@end#import "User.h"#import "Ident.h"@implementation User// 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。+ (NSDictionary *)modelContainerPropertyGenericClass { return @{@"ident" : [Ident class]};}@end
2.使用方法(yy_modelWithJSON、yy_modelToJSONObject)
yy_modelWithJSON:将 JSON (NSData,NSString,NSDictionary) 转换为 Model
yy_modelToJSONObject:将Model转换成NSDictionary以及NSArray
User *user = [User yy_modelWithJSON:jsonString]; NSLog(@"%@",user.ident[0].priceDate); // 将 Model 转换为 JSON 对象: NSDictionary *json = [user yy_modelToJSONObject];
0 0
- iOS Json转换模型库:YYModel
- ios本地文件读取,json解析YYModel
- iOS:YYModel
- 网络:YYModel 使用(JSON到模型的转换)
- iOS:使用YYModel实现本地JSON文件解析
- 自定义YYModel多层转换
- iOS YYModel使用方法
- iOS:YYModel的简单使用
- iOS源码解析—YYModel(NSObject+YYModel)
- 【iOS】json格式转换
- ios NSDictionary/JSON转换
- iOS json格式转换
- iOS源码解析—YYModel(YYClassInfo)
- iOS中YYModel的常见用法
- iOS YYModel MJExtension JSONModel ----简单学习使用
- ios json 与JSON ,NSDictionary相互转换
- iOS JSON Model 转换 MJExtension
- iOS里的JSON转换
- Android 中this、 getApplicationContext()、getApplication()之间的区别
- VS2010 C++下编译调试MongoDB源码
- Keras(2):使用Keras构建神经网络进行Mnist手写字体分类,并定性分析各种超参数的影响
- MyEclipse下连接SVN资源库URL连接及验证错误解决办法
- CodeForces600B(二分查找)
- iOS Json转换模型库:YYModel
- @weakify, @strongify
- 关于智能指针
- 初级还是高级开发者,到底区别在哪里?
- 这个控件找不到的问题.报错瞎报..
- Linux下C++调用Java简单实例(jni)
- java生成指定范围的随机数
- Java 简单加法器的实现
- RxJava之Schedulers详解