Latest 1.0.4
Homepage https://github.com/javenl/JsonReciprocity
License MIT
Platforms ios 6.0, requires ARC
Authors
  • A wonderful framework for converting between JsonString and JsonObject. Convenient、Flexible、Easy to Use.
    方便、灵活、好用的Json与Object互转框架。

  • The framework is Category of NSObject, don’t need to extends any class.
    框架是NSObject的分类,不需要继承任何基类即可使用。

  • If you like the framework or the framework help you a little, please make a star, which is great inspire for me.

  • 如果你喜欢这个框架,或者这个框架能给你一点点帮助,希望你可以收藏一下,你的行动就是对我最大的支持。

Quick start 【快速使用】

Easy to use with 7 methods. 【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]];

目录

Setup 【安装】

Pod 【通过pod管理】

pod 'JsonReciprocity', '~> 1.0.0'

Manually 【手动管理】

1.Download the JSONModel repository as a zip file or clone it.【下载源代码】

2.Copy the /JsonReciprocity/JsonReciprocity into your Xcode project.【把/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

Get start 【使用】

Define 【设定】

JsonString and JsonObject in the example.【例子中使用的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

@implementation PersonModel
+ (NSDictionary *)classReferenceDictForArray {
    return @{@"cars": [CarModel class]};
}
@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

Common usage 【基本用法】

#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);
}

JsonString -> JsonObject

- (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);
}

JsonObject -> 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【Delegate】

More flexible when using JsonReciprocityDelegate.
【通过实现JsonReciprocityDelegate,可以有更多灵活的用法。】

classReferenceDictForArray

If the json object contains another object array, it is necessary to indicate what class it need to reference. Otherwise, it is a NSArray with NSDictionary.
【如果需要转换的JsonObject中又包含了其他对象的数组,需要指定该数组应该自动转换成什么类型的对象数组,否则就映射成一个字典数组。】

+ (NSDictionary *)classReferenceDictForArray {
    return @{@"cars": [CarModel class]};
}

customReferenceDict

Alias with property name 【指定映射的别名】

{
    "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

@implementation TestModel
+ (NSDictionary *)customReferenceDict {
    return @{
             @"indexIdString": @"index",
             @"personal_info_deatil" : "detail"
             };
}
@end

PS:Only define the custom property, other properties will auto reference. 【不需要把每个变量都写上,只需要写特定的,其他没指定的依然会按照变量名命映射。】

isIgnorePropertyKey:

What propertys should be ignore. 【忽略某些属性】

- (BOOL)isIgnorePropertyKey:(NSString *)key {
    if ([key isEqualToString:@"test"]) {
        return YES;
    }
    return NO;
}

autoUpperCaseToCamelCase

Auto convert UpperCase tp CamelCase, default is YES. If you don’t need to convert, return NO.
【自动把下划线风格转驼峰风格,默认为YES。如果不希望自动转换,可以返回NO】

+ (BOOL)autoUpperCaseToCamelCase {
    return NO;
}

Auto convert Example 【自动转换例子】

Without a word, only to define the model, frameword will help you to convert this Irregular json string.
【你不需要写任何代码,把模型定义好,框架就能帮你转换这些不规则的json字符串。】

{
    "Id" : 111,
    "user_Id" : 4096,
    "car_id" : 1234,
    "lastDate":1430647083,
    "__camel___CASE__tEST__": "this is string"
}
@interface IrregularTestModel : NSObject 

@property (assign, nonatomic) NSInteger id;
@property (assign, nonatomic) NSInteger userId;
@property (assign, nonatomic) NSInteger carId;
@property (strong, nonatomic) NSDate *lastDate;
@property (copy, nonatomic) NSString *camelCaseTest;

@end

customFormat:value:

If value is incorrect auto converting, you can custom value as you want.
【如果无法自动值转换正确类型,可以自定义转换的值】

{
    "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

@implementation TestModel
- (id)customFormat:(NSString *)keyPath value:(id)value {
    if ([keyPath isEqualToString:@"date1"]) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy/MM/dd";
        NSDate *date = [formatter dateFromString:value];
        return date;
    } else if ([keyPath isEqualToString:@"date2"]) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy.MM.dd";
        NSDate *date = [formatter dateFromString:value];
        return date;
    }
    return value;
}
@end

Solution for Some Cases 【特殊情况下的解决办法】

1.One json key to many object keys 【一个json键对应多个object键】

Use customFormat 【通过customFormat】

- (id)customFormat:(NSString *)keyPath value:(id)value {
    if ([keyPath isEqualToString:@"propertyKey"]) {
        self.b = value;
        self.c = value;
    }
    return value;
}

2.Many json keys to one object key 【多个json键对应一个object键】

Use customReferenceDict 【通过customReferenceDict】

+ (NSDictionary *)customReferenceDict {
    return @{
             @"json_key_1": @"propertyKey",
             @"json_key_2": @"propertyKey",
             @"json_key_3": @"propertyKey",
             };
}

PS: Many json keys should be exclusionary.【多个json键之间应该是互斥的,不应该同时出现。】

Compare with other framework【框架对比】

There is a comparison for JsonReciprocity, MJExtension, JSONModel, Mantle.
Convert a complex JsonString to JsonObject for 1, 5 ,10, 20 ,50 times.

JsonReciprocityMJExtensionJSONModelMantle之间的用法对比。】
【一个相对复杂的JsonString转换为JsonObject,转换1、5、10、20、50次所花费的时间,单位秒。】

Framework 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

chart

For data, JsonReciprocity ≈ MJExtension > JSONModel > Mantle, time rate is about 1 : 1.1 : 2.2 : 5.3.

【从测试数据来看,JsonReciprocity ≈ MJExtension > JSONModel > Mantle,时间比大约是1 : 1.1 : 2.2 : 5.3。】

This is only one Tests here, everybody can make more other Tests.

【这只是其中一个例子,可能不太全面,大家可以自行用其他测试例子试试。】

PS:Tests example with data in ObjectSerializationTests.【对比的例子与数据在ObjectSerializationTests

More 【其他用法】

More cases reference to JsonReciporcity Demo and JsonReciporcity Tests 【更多的用法可以查看Demo和Tests】

About (关于)

Writer 【作者信息】

GitHub:Javen
QQ:412775083
Email:[email protected]

Issue 【任何问题】

  • Communication with Email or QQ.
  • If find bugs, feedback to me immediately.
  • If need some feature, feedback to me.
  • If some better idea, feedback to me.
  • If you want contribution, Pull Requests.
  • 想交流的可以加qq和发邮件
  • 如果发现任何bug,希望你立即告诉我
  • 希望有什么新功能,请尽管告诉我
  • 如果用起来觉得那里不爽的,欢迎吐槽我
  • 如果你想为贡献代码,Pull Requests即可

Latest podspec

{
    "name": "JsonReciprocity",
    "version": "1.0.4",
    "summary": "A lightweight framework for converting between Json String and Json Object.",
    "description": "A lightweight and fastest framework for converting between Json String and Json Object. This is a category that don't need to extends any other class. Convenientu3001Flexibleu3001Easy to Use.",
    "homepage": "https://github.com/javenl/JsonReciprocity",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Javen_liu": "[email protected]"
    },
    "platforms": {
        "ios": "6.0"
    },
    "source": {
        "git": "https://github.com/javenl/JsonReciprocity.git",
        "tag": "1.0.4"
    },
    "source_files": "JsonReciprocity/JsonReciprocity/*.{h,m}",
    "requires_arc": true
}

Pin It on Pinterest

Share This