IOS-JSONModel使用

git下载

JSON数据 首先也是最重要的,明白自己要哪些数据。

  "data":[
        {
            "name":"追风少年",
            "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  * server1;
@property(nonatomic,copy)NSString  * server2;
@end

@protocol purposeModel//注意要加上这句
@end
@interface purposeModel :JSONModel
@property(nonatomic,copy)NSString  * purpose1;
@property(nonatomic,copy)NSString  * purpose2;
@end

@interface PlaceListModel : JSONModel
@property(nonatomic,copy)NSString  * name;
@property(nonatomic,copy)NSString  * ID;
@property(nonatomic,copy)NSArray  * purpose;
@property(nonatomic,copy)serverModel  * server;

@end

.m里


@implementation PlaceListModel
//key值的change
+ (JSONKeyMapper *)keyMapper{
    return [[JSONKeyMapper alloc]initWithModelToJSONDictionary:@{@"ID":@"id"}];
}
@end

@implementation serverModel

@end

@implementation purposeModel

@end

设置所有的属性为可选(所有属性值可以为空)

+(BOOL)propertyIsOptional:(NSString*)propertyName
{
  return YES;
}
从接口获取数据的处理
NSError * error ;
_dataArray =[PlaceListModel arrayOfModelsFromData:responseObject[@"data"] error:&error]
NSLog(@"%@",error);

_dataArray对应上面的data整个数组

有的时候我们需要的接口所返回Model里的某个数组 比如上面的
 PlaceListModel * model  = [[PlaceListModel alloc] initWithDictionary:responseObject[@"data"] error:nil];
再把这个model里的purpose赋值给某个数组。
_dataArray = model. purpose 
其实purpose已经是一个独立的Model了

你可能感兴趣的:(IOS-JSONModel使用)