一个在 JsonString 和 JsonObject 之间转换的奇妙框架。方便、灵活、易于使用。
方便、灵活、好用的Json与Object互转框架。
框架是 NSObject 分类,不需要扩展任何类。
框架是NSObject的分类,不需要继承任何基类即可使用。
如果你喜欢这个框架或者这个框架对你有所帮助,请给它加星标,这将给我极大的鼓舞。
如果你喜欢这个框架,或者这个框架能给你一点点帮助,希望你可以珍藏这个,你的行动就是对我最大的支持。
##快速使用 【快速使用】 简单易用的 7 个方法。【7个基本方法】
JsonString -> JsonDictionary
NSDictionary *jsonDictionary = [jsonString toJsonDictionary];
JsonDictionary -> JsonObject
PersonModel *personModel = [PersonModel objectFromJsonDict:jsonDictionary];
JsonString -> JsonObject
PersonModel *personModel = [PersonModel objectFromJsonDict:[jsonString toJsonDictionary]];
JsonObject -> JsonDictionary
NSDictionary *jsonDictionary = [personModel toJsonDictionary];
JsonDictionary -> JsonString
NSString *jsonString = [jsonDictionary toJsonString];
JsonObject -> JsonString
NSString *jsonString = [personModel toJsonString];
JsonString -> JsonObjectArray
NSArray *personModels = [PersonModel objectArrayFromJsonArray:[jsonString toJsonArray]];
##目录
pod 'JsonReciprocity', '~> 1.0.0'
1. 以 zip 文件或克隆的方式下载 JSONModel 仓库。【下载源代码】
2. 将 /JsonReciprocity/JsonReciprocity 目录中的文件复制到您的 Xcode 项目中。【把/JsonReciprocity/JsonReciprocity目录中下的文件加入工程】
JsonReciprocity
├── JsonDeserialization.h
├── JsonDeserializstion.m
├── JsonReciprocity.h
├── JsonSerialization.h
├── JsonSerialization.m
├── NSMutableDictionary+SafeSet.h
├── NSMutableDictionary+SafeSet.m
├── NSObject+JsonReciprocity.h
└── NSObject+JsonReciprocity.m
示例中的 JsonString 和 JsonObject。【例子中使用的JsonString和JsonObject】
{ "id": 1420194, "name" : "Jack", "score" : 88.33, "register_date" : 1428647083, "last_login_time" : 1430642742, "house": { "address": "GuangZhou China", "area": 95.6, "tags":[ "nice", "comfort" ]} }, "cars":[{ "brand":"benz", "num":"A14212" }] } |
//PersonModel @interface PersonModel : NSObject @property (nonatomic, assign) NSInteger id; @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) CGFloat score; @property (nonatomic, strong) NSDate *registerDate; @property (nonatomic, assign) NSTimeInterval lastLoginTime; @property (nonatomic, strong) NSArray *cars; @property (nonatomic, strong) HouseModel *house; @end
//CarModel @interface CarModel : NSObject @property (copy, nonatomic) NSString *num; @property (copy, nonatomic) NSString *brand; @end //HouseModel @interface HouseModel : NSObject @property (copy, nonatomic) NSString *address; @property (assign, nonatomic) CGFloat area; @property (strong, nonatomic) NSArray *tags; @end |
#import "JsonReciprocity.h"
####JsonString -> JsonDictionary
- (void)jsonStringToJsonDictionary {
NSString *jsonString = @"\
{\
\"id\": 1420194,\
\"name\" : \"Jack\",\
\"score\" : 88.33,\
\"register_date\" : 1428647083,\
\"last_login_time\" : 1430642742,\
\"house\": {\
\"address\": \"GuangZhou China\",\
\"area\": 95.6,\
\"tags\":[\
\"nice\",\
\"comfort\"\
]}\
},\
\"cars\":[{\
\"brand\":\"benz\",\
\"num\":\"A14212\"\
}]\
}";
NSDictionary *jsonDictionary = [jsonString toJsonDictionary];
NSLog(@"jsonString: %@", jsonString);
NSLog(@"jsonDictionary: %@", jsonDictionary);
}
####JsonDictionary -> JsonObject
- (void)jsonDictionaryToJsonObject {
NSDictionary *jsonDictionary = @{
@"id":@"1420194",
@"name" :@"Jack",
@"score" : @"88.33",
@"register_date": @"1428647083",
@"last_login_time" : @"1430642742",
@"house" : @{
@"address" : @"GuangZhou China",
@"area" : @(95.6),
@"tags" : @[@"nice",
@"comfort"]
},
@"cars" : @[@{
@"brand":@"benz",
@"num":@"A14212"
}]
};
PersonModel *personModel = [PersonModel objectFromJsonDict:jsonDictionary];
NSLog(@"jsonDictionary: %@", jsonDictionary);
NSLog(@"jsonObject: %@", personModel);
}
- (void)jsonStringToJsonObject {
NSString *jsonString = @"\
{\
\"id\": 1420194,\
\"name\" : \"Jack\",\
\"score\" : 88.33,\
\"register_date\" : 1428647083,\
\"last_login_time\" : 1430642742,\
\"house\": {\
\"address\": \"GuangZhou China\",\
\"area\": 95.6,\
\"tags\":[\
\"nice\",\
\"comfort\"\
]}\
},\
\"cars\":[{\
\"brand\":\"benz\",\
\"num\":\"A14212\"\
}]\
}";
PersonModel *personModel = [PersonModel objectFromJsonDict:[jsonString toJsonDictionary]];
NSLog(@"jsonString: %@", jsonString);
NSLog(@"jsonModel: %@", personModel);
}
####JsonObject -> JsonDictionary
- (void)jsonOjectToJsonDictionary {
PersonModel *personModel = [[PersonModel alloc] init];
personModel.id = 1420194;
personModel.name = @"Jack";
personModel.score = 88.33;
personModel.registerDate = [NSDate dateWithTimeIntervalSince1970:1428647083];
HouseModel *house = [[HouseModel alloc] init];
house.address = @"GuangZhou China";
house.area = 95.6;
house.tags = @[@"nice", @"comfort"];
personModel.house = house;
CarModel *car = [[CarModel alloc] init];
car.brand = @"benz";
car.num = @"A14212";
personModel.cars = @[car];
NSDictionary *jsonDictionary = [personModel toJsonDictionary];
NSLog(@"jsonObject: %@", personModel);
NSLog(@"jsonDictionary: %@", jsonDictionary);
}
####JsonDictionary -> JsonString
- (void)jsonDictionaryToJsonString {
NSDictionary *jsonDictionary = @{
@"id":@"1420194",
@"name" :@"Jack",
@"score" : @"88.33",
@"register_date": @"1428647083",
@"last_login_time" : @"1430642742",
@"house" : @{
@"address" : @"GuangZhou China",
@"area" : @(95.6),
@"tags" : @[@"nice",
@"comfort"]
},
@"cars" : @[@{
@"brand":@"benz",
@"num":@"A14212"
}]
};
NSString *jsonString = [jsonDictionary toJsonString];
NSLog(@"jsonDictionary: %@", jsonDictionary);
NSLog(@"jsonString: %@", jsonString);
}
- (void)jsonObjectToJsonString {
PersonModel *personModel = [[PersonModel alloc] init];
personModel.id = 1420194;
personModel.name = @"Jack";
personModel.score = 88.33;
personModel.registerDate = [NSDate dateWithTimeIntervalSince1970:1428647083];
HouseModel *house = [[HouseModel alloc] init];
house.address = @"GuangZhou China";
house.area = 95.6;
house.tags = @[@"nice", @"comfort"];
personModel.house = house;
CarModel *car = [[CarModel alloc] init];
car.brand = @"benz";
car.num = @"A14212";
personModel.cars = @[car];
NSString *jsonString = [personModel toJsonString];
NSLog(@"jsonObject: %@", personModel);
NSLog(@"jsonString: %@", jsonString);
}
####JsonString -> JsonObjectArray
- (void)jsonStringToJsonObjectArray {
NSString *jsonString = @"\
[{\
\"id\": 1420194,\
\"name\" : \"Jack\",\
\"score\" : 88.33,\
\"register_date\" : 1428647083,\
\"last_login_time\" : 1430642742,\
\"house\": {\
\"address\": \"GuangZhou China\",\
\"area\": 95.6,\
\"tags\":[\
\"nice\",\
\"comfort\"\
]}\
},\
\"cars\":[{\
\"brand\":\"benz\",\
\"num\":\"A14212\"\
}]\
},{\
\"id\": 1420194,\
\"name\" : \"Jack\",\
\"score\" : 88.33,\
\"register_date\" : 1428647083,\
\"last_login_time\" : 1430642742,\
\"house\": {\
\"address\": \"GuangZhou China\",\
\"area\": 95.6,\
\"tags\":[\
\"nice\",\
\"comfort\"\
]}\
},\
\"cars\":[{\
\"brand\":\"benz\",\
\"num\":\"A14212\"\
}]\
}]";
NSArray *personModels = [PersonModel objectArrayFromJsonArray:[jsonString toJsonArray]];
NSLog(@"jsonString: %@", jsonString);
NSLog(@"jsonObjects: %@", personModels);
}
###JsonReciprocityDelegate【委托】
使用 JsonReciprocityDelegate 时更加灵活。
【通过实现 JsonReciprocityDelegate,可以拥有更多灵活的用法。】
####classReferenceDictForArray
如果JsonObject包含另一个对象数组,需要指定它应该自动转换成什么类型的对象数组,否则就映射成一个字典数组。
【如果需要转换的JsonObject中又包含了其他对象的数组,需要指定该数组应该自动转换成什么类型的对象数组,否则就映射成一个字典数组。】
+ (NSDictionary *)classReferenceDictForArray {
return @{@"cars": [CarModel class]};
}
指定映射的别名 【指定映射的别名】
{ "indexIdString" : @"111", "name" : @"jack", "personal_info_deatil" : @"A nice man" } |
@interface TestModel : NSObject @property (assign, nonatomic) NSInteger index; @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *detail; @end
|
PS:只需要定义自定义属性,其他属性将自动引用。【不需要将每个变量都写出来,只需要写特定的,其他未指定的变量将按照变量名映射。】
应该忽略哪些属性。【忽略某些属性】
- (BOOL)isIgnorePropertyKey:(NSString *)key {
if ([key isEqualToString:@"test"]) {
return YES;
}
return NO;
}
自动将大写转换为驼峰式,默认为YES。如果您不需要转换,返回NO。
【自动将下划线风格转换为驼峰风格,默认为YES。如果不希望自动转换,可以返回NO】
+ (BOOL)autoUpperCaseToCamelCase {
return NO;
}
自动转换示例:【自动转换例子】
无需任何代码,只需定义好模型,框架就能帮助您转换这些不规则的JSON字符串。【你不需要写任何代码,把模型定义好,框架就能帮你转换这些不规则的JSON字符串。】
{ "Id" : 111, "user_Id" : 4096, "car_id" : 1234, "lastDate":1430647083, "__camel___CASE__tEST__": "this is string" } |
@interface IrregularTestModel : NSObject |
如果值无法自动转换为正确的类型,您可以自定义转换的值。
【如果无法自动值转换正确类型,可以自定义转换的值】
{ "date1" : "2015/07/11", "date2" : "2015.05.29", "content_detail" : "this is a detail", } |
@interface TestModel : NSObject @property (strong, nonatomic) NSDate *date1; @property (strong, nonatomic) NSDate *date2; @property (strong, nonatomic) NSString *str; @end
|
####1.一个JSON键对应多个对象键:【一个json键对应多个object键】
使用customFormat:【通过customFormat】
- (id)customFormat:(NSString *)keyPath value:(id)value {
if ([keyPath isEqualToString:@"propertyKey"]) {
self.b = value;
self.c = value;
}
return value;
}
####2.多个JSON键对应一个对象键:【多个json键对应一个object键】
使用customReferenceDict:【通过customReferenceDict】
+ (NSDictionary *)customReferenceDict {
return @{
@"json_key_1": @"propertyKey",
@"json_key_2": @"propertyKey",
@"json_key_3": @"propertyKey",
};
}
PS:多个JSON键应该是互斥的。【多个json键之间应该是互斥的,不应该同时出现。】
有关于JsonReciprocity
、MJExtension
、JSONModel
、Mantle
的对比。
将一个复杂的JsonString转换为JsonObject,1次、5次、10次、20次、50次的时间。
【JsonReciprocity
、MJExtension
、JSONModel
、Mantle
之间的用法对比。】
【一个相对复杂的JsonString转换为JsonObject,转换1次、5次、10次、20次、50次所花费的时间,单位秒。】
框架 | 1 | 5 | 10 | 20 | 50 |
---|---|---|---|---|---|
JsonReciprocity | 0.00257 | 0.01566 | 0.01882 | 0.04048 | 0.09789 |
MJExtension | 0.00292 | 0.01692 | 0.02674 | 0.04568 | 0.11325 |
JsonModel | 0.00553 | 0.02781 | 0.05554 | 0.09780 | 0.23649 |
Mantel | 0.01668 | 0.06899 | 0.12888 | 0.23499 | 0.53936 |
对于数据,JsonReciprocity ≈ MJExtension > JSONModel > Mantle,时间比大约是 1 : 1.1 : 2.2 : 5.3。
【从测试数据来看,JsonReciprocity ≈ MJExtension > JSONModel > Mantle,时间比大约是 1 : 1.1 : 2.2 : 5.3。】
这里只有一个测试例子,大家可以做更多测试。
【这只是其中一个例子,可能不太全面,大家可以自行用其他测试例子试试。】
PS:与数据在 ObjectSerializationTests
中进行的测试例子进行对比。【对比的例子与数据在ObjectSerializationTests
】
更多案例参考JsonReciporcity Demo
和JsonReciporcity Tests
。【更多的用法可以查看Demo和Tests】
###作者信息 GitHub:Javen
QQ:412775083
Email:[email protected]