@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
其中:json
Optional關鍵字表示testArray屬性能夠爲空。當server端沒有返回對應的數據時,忽略對它的解析。沒有聲明可選的屬性,若是server端沒有返回對應的數據,會引發crash。數組
entity2是Test2Entity類型的屬性,Test2Entity必須是JSONModel的子類。會自動嵌套解析直到所有遍歷完。app
調用:ui
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];
其中:atom
initWithDictionary:方法把傳入的字典自動解析生成Entity。若是不特別設定,默認數據源的key和屬性名相同。spa
toJSONString方法把對象解析成JSONString,toDictionary方法把對象解析成字典。code
typedef NS_ENUM(NSInteger, TestEnum) { TestENUM_1 = 0, TestENUM_2 = 1, TestENUM_3 = 2 }; @interface Test2Entity : JSONModel @property (nonatomic, assign) TestEnum testEnum; @end
調用:server
Test2Entity *entity = [[Test2Entity alloc] initWithDictionary:@{@"testEnum":@2} error:nil]; NSString *jsonString = [entity toJSONString];
@interface Test3Entity : JSONModel @property (nonatomic, strong) Guid *myId; @end
調用:對象
Test3Entity *entity = [[Test3Entity alloc] initWithDictionary:@{@"myId":@"60165555201222841131397181021744"} error:nil]; NSString *jsonString = [entity toJSONString];
@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。
想要讓一個數組內的對象自動解析,須要作一些特殊處理。
一、須要在定義Entity的時候,定義一個與Entity同名的protocol。
二、須要在定義數組屬性時,聲明這個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];
默認狀況下,會根據屬性的名字去匹配返回數據的Key。若是類的屬性名和返回數據的Key不相同,則須要經過手動指定來匹配。JSONModel庫經過JSONKeyMapper類來記錄這些對應關係。
在JSONModel類中,有一個類方法+(JSONKeyMapper*)keyMapper;返回的JSONKeyMapper對象在JSONModel子類對象解析數據的時候被使用。
+(JSONKeyMapper *)keyMapper { JSONKeyMapper *keyMapper = [[JSONKeyMapper alloc] initWithDictionary:@{@"ServerKey":@"ClassProperty"}]; return keyMapper; }
每一個JSONModel子類都這樣弄會比較繁瑣。當屬性名和Key之間的對應關係是有必定規律時,可使用JSONModel的另外幾個類方法來快速設定。
一、Key首字母大寫,屬性名首字母小寫
+(JSONKeyMapper *)keyMapper { JSONKeyMapper *keyMapper = [JSONKeyMapper mapperFromUpperCaseToLowerCase]; return keyMapper; }
二、key首字母是下劃線,後面和屬性名相同
+(JSONKeyMapper *)keyMapper { JSONKeyMapper *keyMapper = [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase]; return keyMapper; }