JSONModel是一个库,让你快速创建智能数据模型。您可以在iOS和OSX应用程序使用它。
JSONModel自动内部检查你的模型类和你的JSON投入的结构,大大降低了代码,你必须编写量。
注意下面这张图:这意味着JSON的数据格式只有中间的部分, string,number, array, object, 以及null
添加JSONModel到您的项目
要求
- ARC; 的iOS 5.0+ / OSX 10.7+
- SystemConfiguration.framework
得到它为:
1)源文件
- 下载JSONModel库 zip文件或复制它
- 把JSONModel子文件夹复制到您的Xcode项目
2)通过pods
在项目的文件夹,使用终端输入下面的命令:
pod 'JSONModel'
源代码文档
源代码包含类的文档,你可以建立自己和导入Xcode中:
- 如果您还没有安装开发文档,那么就自己导入吧。
- 安装文件到Xcode的开发文档目录
,
在库的根目录下。 - 重新启动的Xcode。
基本用法
如果你有一个这样的JSON数据:
{"id":"10", "country":"Germany", "dialCode": 49, "isInEurope":true}
- 为您的数据模型,创建一个新的Objective-C类,并使其继承JSONModel类。
- 与JSON键的名称你的头文件中声明的属性:
#import "JSONModel.h"
@interface CountryModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* country;
@property (strong, nonatomic) NSString* dialCode;
@property (assign, nonatomic) BOOL isInEurope;
@end
一些没有必要的东西可以放在.M文件。
#import "CountryModel.h"
...
NSString* json = (fetch here JSON from Internet) ...
NSError* err = nil;
CountryModel* country = [[CountryModel alloc] initWithString:json error:&err];
如果JSON的验证通过你在你的模型中填充的JSON所有相应的属性。JSONModel也将尝试上述它将尽可能多的数据转换为您所期望的类型,在这个例子:
- 从字符串“ID”(在JSON中)转换为int的类
- 只是复制country的价值
- 转换数字dialCode(在JSON中)以一个NSString值
- 最终转换isInEurope为BOOL为您BOOL财产
而好消息是,你所要做的就是定义属性及其预期的类型。
在线教程
官方网站:http://www.jsonmodel.com
类文档在线:http://jsonmodel.com/docs/
下面是例子
AUTOMATIC NAME BASED MAPPING
{
"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)
{
"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
{
"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
{
"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
//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
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];
CUSTOM DATA TRANSFORMERS
@implementation JSONValueTransformer (CustomTransformer)
- (NSDate *)NSDateFromNSString:(NSString*)string {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:APIDateFormat];
return [formatter dateFromString:string];
}
- (NSString *)JSONObjectFromNSDate:(NSDate *)date {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:APIDateFormat];
return [formatter stringFromDate:date];
}
@end
JSON验证 错误处理 自定义数据验证 自动比较和平等的功能
Misc
Author: Marin Todorov
Contributors: Christian Hoffmann, Mark Joslin, Julien Vignali, Symvaro GmbH, BB9z. Also everyone who did successful pull requests.
Change log : https://github.com/icanzilb/JSONModel/blob/master/Changelog.md
LICENSE
This code is distributed under the terms and conditions of the MIT license.
CONTRIBUTION GUIDELINES
NB! If you are fixing a bug you discovered, please add also a unit test so I know how exactly to reproduce the bug before merging.