1. 命名自动匹配
a. Model属性名和服务器返回数据字段一致
{
@"id":"123",
@"name":"巧克力",
@"price":"6.66"
}
@interface ProductModel : BaseModel
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) float price;
@end
2. Model关联(model含有其它model)
a. Model属性名和服务器返回数据一致;
b. 关联其它Model的属性需要指定自身类型(例如:ProductModel *)
{
“order_id”:104,
“total_price”:110,
“product”: {
“id”:”123”,
“name”:”productName”,
“price”:”110”,
}
}
@interface OrderModel : BaseModel
@property (nonatomic, strong) NSString *order_id;
@property (nonatomic, assign) float total_price;
@property (nonatomic, strong) ProductModel *product;
@end
3. Model集合(Model含有其它model的集合)
a. NSArry后的就<>中包含一个协议,使用JSONModel必须实现这个协议!
b. 层级嵌套,Model中包含其他Model集合,必须实现该协议
c. 包含其他Model集合的属性需要指定层级类型和自身类型(例如:NSArray<ProductModel> *)
@protocol ProductModel
@end
@interface OrderModel : JSONModel
@property (nonatomic, strong) NSString *order_id;
@property (nonatomic, assign) float total_price;
@property (nonatomic, strong) NSArray<ProductModel> *products;
@end
4. 键映射
a. 在一个Model中获取服务器返回数据不同层级的数据;
b. 例如:order_id和name不在同一层级;
c. 采用KVC的方式来取值,并赋值给Model属性(order_details.name ---> productName order_details.price.usd ---> price);
{
"order_id": 104,
"order_details" : [
{
"name": "name_#1",
"price": {
"usd": 12.95
}
}
]
}
@interface OrderModel : BaseModel
@property (nonatomic, strong) NSString *id;
@property (nonatomic, assign) float price;
@property (nonatomic, strong) NSString *productName;
@end
@implementation OrderModel
+ (JSONKeyMapper *)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{
@"order_id": @"id",
@"order_details.name": @"productName", @"order_details.price.usd": @"price"}];
}
@end
d.全局Global key mapping. (使所有的模型都具备)
JSONKeyMapper *keyMapper = [[JSONKeyMapper alloc] initWithDictionary:@{@"id":@"modelId"}];
[BaseModel setGlobalKeyMapper:keyMapper];
5. 设置下划线自动转驼峰
a. 自定义把下划线字段解析为驼峰命名属性
b. 场景:服务器数据返回下划线命名字段可为Model中以驼峰命名的属性相应的赋值
c. mapperFromUpperCaseToLowerCase 大写转小写
{
"order_id": 104,
"order_product" : @"Product#1",
"order_price" : 12.95
}
@interface OrderModel : BaseModel
@property (nonatomic, strong) NSString *orderId;
@property (nonatomic, assign) float orderPrice;
@property (nonatomic, strong) NSString *orderProduct;
@end
@implementation OrderModel
+ (JSONKeyMapper *)keyMapper
{
return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
}
@end
6. 可选属性(属性值可以为空或null)
a. 某些属性值可以为空;
b. 防止由于服务器返回数据为空导致JSONModel异常(程序崩溃);
eg: @property (nonatomic, strong) NSString<Optional> *urlString;
7. 忽略属性(属性值可以完全忽略)
a. 解析时完全忽略它;
b. 场景:该属性不需要从服务器数据中获取;
eg: @property (nonatomic, strong) NSString<Ignore> *urlString;
8. 设置所有属性可选(所有属性值可以为空)
a. Model 的所有属性值可以为空;
b. 防止由于服务器数据返回空导致JSONModel异常(程序崩溃);
c. 官方建议尽量避免使用该方法(即使要全部属性为可选,也尽量是在每个属性那里标注为Optional);
eg:
@implementation HomeHotModel
+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
return YES;
}
@end
9. 将Model导出字典或JSON字符串
a. 快速导出Model中所有属性和属性值;
eg:
HomeHotModel *model = [[HomeHotModel alloc] initWithString:jsonString error:nil];
//以字典形式导出
NSDictionary *dict = [model toDictionary];
//以字符串形式导出
NSString *string = [model toJSONString];
10. 自定义数据处理(JSON数据格式:string, number, array, object, 以及null)
a. 添加NSDate转换:根据后台返回时间戳格式进行相应处理(eg:UIColor);
b. 场景:内嵌转换,不能满足需求,需要自定义;
c. JSONModel支持类型如下图:
eg:
#import "JSONValueTransformer+CustomTransformer.h"
@implementation JSONValueTransformer (CustomTransformer)
//时间戳转NSDate
- (NSDate *)NSDateFromNSString:(NSString *)string
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:string];
return date;
}
//NSDate转时间戳
- (NSString *)JSONObjectFromNSDate:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateStr = [formatter stringFromDate:date];
return dateStr;
}
@end
11. 延迟加载, 这种比较推荐,可以减少在网络读取时的性能消耗,关键字为: ConvertOnDemand
{
"order_id": 104,
"total_price": 103.45,
"products" : [
{
"id": "123",
"name": "Product #1",
"price": 12.95
},
{
"id": "137",
"name": "Product #2",
"price": 82.95
}
]
}
使用模型:
@protocol ProductModel
@end
//产品Model
@interface ProductModel : JSONModel
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) float price;
@end
//订单Model
@interface OrderModel : JSONModel
@property (nonatomic, strong) NSString *order_id;
@property (nonatomic, assign) float total_price;
@property (nonatomic, strong) NSArray<ProductModel, ConvertOnDemand> *products;
@end
12. 模型的批处理,即一次可以处理一批模型
NSArray *models = [OrderModel arrayOfObjectsFromDictionaries:jsonDatas error:nil];