JSONModel库的使用方法

使用方法

1、基本类型/字符串/普通数组/字典/嵌套/可选

@interface  Test1Entity : JSONModel

@property (nonatomic, strong) NSString              *testString;
@property (nonatomic, assign) int                   testNumber;
@property (nonatomic, strong) NSArray<Optional>     *testArray;
@property (nonatomic, strong) NSDictionary          *testDictionary;
@property (nonatomic, strong) Test2Entity           *entity2;

@end

其中:

      Optional关键字表示testArray属性可以为空。当server端没有返回对应的数据时,忽略对它的解析。没有声明可选的属性,如果server端没有返回对应的数据,会引起crash。

      entity2是Test2Entity类型的属性,Test2Entity必须是JSONModel的子类。会自动嵌套解析直到全部遍历完。

调用:

Test1Entity *entity = [[Test1Entity alloc] initWithDictionary:@{@"testString"       :@"abc",
                                                                @"testNumber"       :@12,
                                                                @"testArray"        :@[@"1"],
                                                                @"testDictionary"   :@{@"key":@"value"},
                                                                @"entity2"          :@{@"testEnum":@2}
                                                                }
                                                        error:nil];
NSString *jsonString = [entity toJSONString];
NSDictionary *dict = [entity toDictionary];

其中:

      initWithDictionary:方法把传入的字典自动解析生成Entity。如果不特别设定,默认数据源的key和属性名相同。

      toJSONString方法把对象解析成JSONString,toDictionary方法把对象解析成字典。

2、枚举

typedef NS_ENUM(NSInteger, TestEnum) {
    TestENUM_1 = 0,
    TestENUM_2 = 1,
    TestENUM_3 = 2
};

@interface Test2Entity : JSONModel

@property (nonatomic, assign) TestEnum testEnum;

@end

调用:

Test2Entity *entity = [[Test2Entity alloc] initWithDictionary:@{@"testEnum":@2} error:nil];
NSString *jsonString = [entity toJSONString];

3、Guid

@interface Test3Entity : JSONModel

@property (nonatomic, strong) Guid *myId;

@end

调用:

Test3Entity *entity = [[Test3Entity alloc] initWithDictionary:@{@"myId":@"60165555201222841131397181021744"} error:nil];
NSString *jsonString = [entity toJSONString];

4、NSDate

@interface Test4Entity : JSONModel

@property (nonatomic, strong) NSDate    *myDate;

@end

调用:

Test4Entity *entity = [[Test4Entity alloc] initWithDictionary:@{@"myDate":@"2014-01-20 12:30:33"} error:nil];
NSString *jsonString = [entity toJSONString];

      对于NSDate属性的解析,已经自动做了本地时间和UTC时间的转换。当数据向对象转换,UTC - Local。当对象向数据转换,Local - UTC。

5、对象数组/延迟加载

      想要让一个数组内的对象自动解析,需要做一些特殊处理。

    1、需要在定义Entity的时候,定义一个与Entity同名的protocol。

    2、需要在定义数组属性时,声明这个protocol。

typedef NS_ENUM(NSInteger, TestEnum) {
    TestENUM_1 = 0,
    TestENUM_2 = 1,
    TestENUM_3 = 2
};

@protocol Test2Entity
@end

@interface Test2Entity : JSONModel

@property (nonatomic, assign) TestEnum testEnum;

@end
//--------------------------------------------------------------
@interface Test6Entity : JSONModel

@property (nonatomic, strong) NSArray<Optional, Test2Entity, ConvertOnDemand>  *entityArray;

@end

调用:

Test6Entity *entity = [[Test6Entity alloc] initWithDictionary:@{@"entityArray":@[@{@"testEnum":@2}, @{@"testEnum":@3}]} error:nil];
NSString *jsonString = [entity toJSONString];
NSDictionary *dict = [entity toDictionary];

6、server端返回数据的Key和Class的属性之间的对应关系设置

      默认情况下,会根据属性的名字去匹配返回数据的Key。如果类的属性名和返回数据的Key不相同,则需要通过手动指定来匹配。JSONModel库通过JSONKeyMapper类来记录这些对应关系。

      在JSONModel类中,有一个类方法+(JSONKeyMapper*)keyMapper;返回的JSONKeyMapper对象在JSONModel子类对象解析数据的时候被使用。

+(JSONKeyMapper *)keyMapper
{
    JSONKeyMapper *keyMapper = [[JSONKeyMapper alloc] initWithDictionary:@{@"ServerKey":@"ClassProperty"}];
    return keyMapper;
}

    每个JSONModel子类都这样弄会比较繁琐。当属性名和Key之间的对应关系是有一定规律时,可以使用JSONModel的另外几个类方法来快速设定。

1、Key首字母大写,属性名首字母小写

+(JSONKeyMapper *)keyMapper
{
    JSONKeyMapper *keyMapper = [JSONKeyMapper mapperFromUpperCaseToLowerCase];
    return keyMapper;
}

2、key首字母是下划线,后面和属性名相同

+(JSONKeyMapper *)keyMapper
{
    JSONKeyMapper *keyMapper = [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
    return keyMapper;
}


转载于:https://my.oschina.net/u/1418722/blog/471282

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值