JSONModel
https://github.com/icanzilb/JSONModel/
一. 获取属性的元数据
const char *attrs = property_getAttributes(property);
propertyAttributes=T@"NSArray<OrderDetail><Optional>",&,N,V_OrderDetails
1. T
T表示普通属性
R表示只读属性
Tc,表示BOOL
2. @"NSArray<OrderDetail><Optional>"
@里面"<"之前是类型
@里面"<"和">"之间是协议
3. V_OrderDetails
V表示变量
后面的OrderDetails表示变量名称
二.获取对象的父类
obj.superclass
[isKindOfClass:class]
原文地址:http://www.cnblogs.com/YouXianMing/p/3638648.html使用 JSONModelPosted on 2014-04-01 20:30 游贤明 阅读(205) 评论(0) 编辑 收藏 |
|||||||||||||||||||
{
"id": "123",
"name": "Product name",
"price": 12.95
}
|
@interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString* name; @property (assign, nonatomic) float price; @end @implementation ProductModel @end |
Model cascading (models including other models)
model中含有其他的model
{
"order_id": 104,
"total_price": 13.45,
"product" : {
"id": "123",
"name": "Product name",
"price": 12.95
}
}
|
@interface OrderModel : JSONModel @property (assign, nonatomic) int order_id; @property (assign, nonatomic) float total_price; @property (strong, nonatomic) ProductModel* product; @end @implementation OrderModel @end |
Model collections
model中含有其他model的集合
{
"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 @interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString* name; @property (assign, nonatomic) float price; @end @implementation ProductModel @end @interface OrderModel : JSONModel @property (assign, nonatomic) int order_id; @property (assign, nonatomic) float total_price; @property (strong, nonatomic) NSArray<ProductModel>* products; @end @implementation OrderModel @end |
Key mapping
键值转回匹配
{
"order_id": 104,
"order_details" : [
{
"name": "Product#1",
"price": {
"usd": 12.95
}
}
]
}
|
@interface OrderModel : JSONModel
@property (assign, nonatomic) int id;
@property (assign, nonatomic) float price;
@property (strong, nonatomic) NSString* productName;
@end
@implementation OrderModel
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{
@"order_id": @"id",
@"order_details.name": @"productName",
@"order_details.price.usd": @"price"
}];
}
@end
|
Global key mapping (applies to all models in your app)
设置全局的键值转回匹配
[JSONModel setGlobalKeyMapper:[
[JSONKeyMapper alloc] initWithDictionary:@{
@"item_id":@"ID",
@"item.name": @"itemName"
}]
];
|
Map automatically under_score case to camelCase
将下滑线转换成首字母大写
{
"order_id": 104,
"order_product" : @"Product#1",
"order_price" : 12.95
}
|
@interface OrderModel : JSONModel
@property (assign, nonatomic) int orderId;
@property (assign, nonatomic) float orderPrice;
@property (strong, nonatomic) NSString* orderProduct;
@end
@implementation OrderModel
+(JSONKeyMapper*)keyMapper
{
return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
}
@end
|
Optional properties (i.e. can be missing or null)
可以为空的属性值
{
"id": "123",
"name": null,
"price": 12.95
}
|
@interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString<Optional>* name; @property (assign, nonatomic) float price; @property (strong, nonatomic) NSNumber<Optional>* uuid; @end @implementation ProductModel @end |
Ignored properties (i.e. JSONModel completely ignores them)
忽略某些属性
{
"id": "123",
"name": null
}
|
@interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString<Ignore>* customProperty; @end @implementation ProductModel @end |
Make all model properties optional (avoid if possible)
让所有的属性都可以有空的属性值
@implementation ProductModel
+(BOOL)propertyIsOptional:(NSString*)propertyName
{
return YES;
}
@end
|
Lazy convert collection items from dictionaries to models
将集合元素转换成 model
{
"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 @interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString* name; @property (assign, nonatomic) float price; @end @implementation ProductModel @end @interface OrderModel : JSONModel @property (assign, nonatomic) int order_id; @property (assign, nonatomic) float total_price; @property (strong, nonatomic) NSArray<ProductModel, ConvertOnDemand>* products; @end @implementation OrderModel @end |
Using the built-in thin HTTP client
使用内置的 HTTP 链接
//add extra headers
[[JSONHTTPClient requestHeaders] setValue:@"MySecret" forKey:@"AuthorizationToken"];
//make post, get requests
[JSONHTTPClient postJSONFromURLWithString:@"http://mydomain.com/api"
params:@{@"postParam1":@"value1"}
completion:^(id json, JSONModelError *err) {
//check err, process json ...
}];
Export model to NSDictionary or to JSON text
将 model 导出为字典或者json字符串
ProductModel* pm = [[ProductModel alloc] initWithString:jsonString error:nil];
pm.name = @"Changed Name";
//convert to dictionary
NSDictionary* dict = [pm toDictionary];
//convert to text
NSString* string = [pm toJSONString];
- json validation
- data transformations
- error handling
- custom data validation
- automatic compare and equality features
- and more.
- json数据键值匹配
- 数据转换
- 好的容错能力
- 自定义数据键值匹配
- 自动比较以及判断的特性
- 还有更多的等待亲来挖掘
以下是本人使用的测试结果




