覺(jué)得有用麻煩給個(gè)喜歡菠隆,謝謝各位親??
我們?cè)陂_發(fā)的項(xiàng)目的時(shí)候,需要很多model來(lái)存儲(chǔ)數(shù)據(jù)狂秘,通常JSONModel使用的比較多骇径,現(xiàn)在就來(lái)簡(jiǎn)單介紹一下JSONModel的使用。
首先配上在網(wǎng)絡(luò)請(qǐng)求中模型轉(zhuǎn)化的代碼:
FirstModel *result = [[FirstModel alloc] initWithDictionary:responseDic error:&error];
注意一:<一定要加這個(gè)方法呀者春,不然解析不出來(lái)破衔,我都被坑了N次了,最好建個(gè)ji lei>
后臺(tái)傳過(guò)來(lái)的參數(shù)钱烟,有時(shí)候是空值晰筛,是null,此時(shí)JSONModel在解析的時(shí)候會(huì)出現(xiàn)崩潰的現(xiàn)象拴袭,此時(shí)我們可以在.m文件中加入一個(gè)類方法:
//這個(gè)方法是把所有的屬性全部設(shè)置為可選值读第,這樣就算后臺(tái)傳過(guò)來(lái)的值是空值,也沒(méi)有關(guān)系
+ (BOOL)propertyIsOptional:(NSString *)propertyName {
return YES;
}
注意二:在末尾
如果后臺(tái)傳過(guò)來(lái)的數(shù)據(jù)是這種形式的:
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
}
這是最簡(jiǎn)單的一種數(shù)據(jù)形式拥刻,我們只需要建立一個(gè)Object-C類怜瞒,繼承自JSONModel,在.h里面寫上和這兩個(gè)對(duì)應(yīng)的屬性就好了般哼,代碼如下:
#import "JSONModel.h"
@interface FirstModel : JSONModel
@property (assign, nonatomic) int status;
@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) NSString* content;
@end
這種情況下吴汪,.m不需要做任何操作,但是蒸眠,如果后臺(tái)闖過(guò)來(lái)的參數(shù)比較復(fù)雜漾橙,我們想自己定義自己習(xí)慣的屬性來(lái)使用,那么我們可以在.m文件中進(jìn)行Key映射楞卡,例子上:
若后臺(tái)傳過(guò)來(lái)的數(shù)據(jù)的名字是這種形式的
{
user_name_type = "\U6210\U529f";
succeed = 1;
}
我們?cè)贔irstModel的.h中的代碼為:
#import "JSONModel.h"
@interface FirstModel : JSONModel
@property (assign, nonatomic) BOOL succeed;
@property (strong, nonatomic) NSString* type;
@end
我們?cè)?m文件中的代碼為
@implementation OrderModel
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{
@"user_name_type": @"type"
}];
}
@end
模型嵌套:
{
msg = "\U6210\U529f";
succeed = 1;
data =
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
}霜运;
}
此時(shí)我們需要?jiǎng)?chuàng)建兩個(gè)繼承自JSONModel的model類,在.h中實(shí)現(xiàn)如下代碼:
@interface SecondModel : JSONModel
@property (assign, nonatomic) BOOL succeed;
@property (strong, nonatomic) NSString* msg;
@property (strong, nonatomic) FirstModel* firstModel;
@end
模型集合:
{
msg = "\U6210\U529f";
succeed = 1;
data = (
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
},
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
}
);
}
創(chuàng)建兩個(gè)繼承自JSONModel的model類臀晃,在.h中實(shí)現(xiàn)如下代碼:
@protocol ThirdModel
@end
@interface ThirdModel : JSONModel
@property (assign, nonatomic) BOOL succeed;
@property (strong, nonatomic) NSString* msg;
@property (strong, nonatomic) NSArray<FirstModel *>* firstModels;
@end
模型嵌套+模型集合(??此處有坑)
{
msg = "\U6210\U529f";
succeed = 1;
data = {
childList = (
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
},
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
}
);
otherChildList= (
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
},
{
content = "\U8f85\U5bfc\U5458\U6307\U5bfc\U6821\U56ed\U5c0f\U8bb0\U8005\U3001\U5c0f\U64ad\U97f3\U5458\U5728\U6821\U56ed\U5e7f\U64ad\U7ad9\n\U91c7\U7f16\U64ad\U4f18\U79c0\U513f\U7ae5\U8d44\U6e90\U5185\U5bb9\U3002";
status = 1;
title = "\U5c11\U5148\U961f\U5458\U4e3b\U64ad";
}
);
};
}
這個(gè)模型比較復(fù)雜觉渴,字典里面套字典介劫,字典中又套用了數(shù)組徽惋,數(shù)組中又是字典,所以我們需要把邏輯整理清楚座韵,首先我們需要新建兩個(gè)繼承自JSONModel的model類分別為ForthModel和FiveModel险绘,在ForthModel的.h中作如下操作:
@interface ForthModel : JSONModel
@property (strong, nonatomic) NSArray< FirstModel *>* otherChildList;
@property (strong, nonatomic) NSArray< FirstModel *>* childList;
@end
同時(shí)踢京,我們需要在.m文件中做一個(gè)操作,不然解析不出來(lái)宦棺,代碼如下:
+ (NSString *)protocolForArrayProperty:(NSString *)propertyName {
if ([propertyName isEqualToString:@"childList"]) {
return NSStringFromClass([FirstModel class]);
}
if ([propertyName isEqualToString:@"otherChildList"]) {
return NSStringFromClass([FirstModel class]);
}
return NSStringFromClass([NSDictionary class]);
}
ForthModel中代碼實(shí)現(xiàn)之后瓣距,實(shí)現(xiàn)FiveModel中的代碼,在.h中做如下操作
@interface FiveModel : JSONModel
@property (assign, nonatomic) BOOL succeed;
@property (strong, nonatomic) NSString* msg;
@property (strong, nonatomic) ForthModel* data;
@end
同時(shí)代咸,我們需要在FiveModel的.m文件中做一個(gè)操作蹈丸,不然依然解析不出來(lái),代碼如下:
+ (NSString *)protocolForArrayProperty:(NSString *)propertyName {
if ([propertyName isEqualToString:@"data"]) {
return NSStringFromClass([ForthModel class]);
}
return NSStringFromClass([NSDictionary class]);
}
注意:一定要在.m文件中加入這個(gè)判斷呐芥,不然解析失敗逻杖。
此時(shí),模型嵌套和模型集合就解析成功思瘟。