git下載
JSON數(shù)據(jù) 首先也是最重要的饿这,明白自己要哪些數(shù)據(jù)盖袭。
"data":[
{
"name":"追風(fēng)少年",
"id":"123",
"server":{
"server1":"xxx",
"server1":"xxx",
},
"purpose":[
{
"purpose1":"xxx"
"purpose2":"xxx"
},
{
"purpose1":"xxx"
"purpose2":"xxx"
}
]
]
.h里
@protocol serverModel//注意要加上這句
@end
@interface server :JSONModel
@property(nonatomic,copy)NSString <Optional> * server1;
@property(nonatomic,copy)NSString <Optional> * server2;
@end
@protocol purposeModel//注意要加上這句
@end
@interface purposeModel :JSONModel
@property(nonatomic,copy)NSString <Optional> * purpose1;
@property(nonatomic,copy)NSString <Optional> * purpose2;
@end
@interface PlaceListModel : JSONModel
@property(nonatomic,copy)NSString <Optional> * name;
@property(nonatomic,copy)NSString <Optional> * ID;
@property(nonatomic,copy)NSArray <Optional,purposeModel> * purpose;
@property(nonatomic,copy)serverModel <Optional> * server;
@end
.m里
@implementation PlaceListModel
//key值的change
+ (JSONKeyMapper *)keyMapper{
return [[JSONKeyMapper alloc]initWithModelToJSONDictionary:@{@"ID":@"id"}];
}
@end
@implementation serverModel
@end
@implementation purposeModel
@end
設(shè)置所有的屬性為可選(所有屬性值可以為空)
+(BOOL)propertyIsOptional:(NSString*)propertyName
{
return YES;
}
從接口獲取數(shù)據(jù)的處理
NSError * error ;
_dataArray =[PlaceListModel arrayOfModelsFromData:responseObject[@"data"] error:&error]
NSLog(@"%@",error);
_dataArray對(duì)應(yīng)上面的data整個(gè)數(shù)組
有的時(shí)候我們需要的接口所返回Model里的某個(gè)數(shù)組 比如上面的
PlaceListModel * model = [[PlaceListModel alloc] initWithDictionary:responseObject[@"data"] error:nil];
再把這個(gè)model里的purpose賦值給某個(gè)數(shù)組。
_dataArray = model. purpose
其實(shí)purpose已經(jīng)是一個(gè)獨(dú)立的Model了