|
| ||||||||||||||||||
原文地址:http://www.cnblogs.com/YouXianMing/p/3638648.html
| ||||||||||||||||||
{
"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数据键值匹配
- 数据转换
- 好的容错能力
- 自定义数据键值匹配
- 自动比较以及判断的特性
- 还有更多的等待亲来挖掘
以下是本人使用的测试结果







1429



被折叠的 条评论
为什么被折叠?




· reddit的运作
· 马云:我的管理和领导方法是中国最好的
· 迅雷通过红十字会向云南地震灾区捐款100万
· 90后毕业生平均月薪2443 三成月光/啃老
· 恨世界的人正在无所事事
» 更多新闻...
· 应用开发的美学之道
· 如何在网页中使用留白
· SQL/NoSQL两大阵营激辩:谁更适合大数据
· 如何获取(GET)一杯咖啡——星巴克REST案例分析