////模型转字典////字典转模型 返回一个mode/////字典转模型 返回多个mode
点我下载demo这个文本的代码
***********随着时间的推移找了新写法(打开左边连接demo,mian.m是使用说明)*******
1.倒入MJExtension
2.模型转字典
使用案例:前端向服务器发送数据的model
//模型转字典
model*mod1 = [[modelalloc]init];
mod1.str1=@"1";
mod1.str2=@"2";
mod1.arr=@[@1,@2,@3,@4,@5];
mod1.dic=@{@"3":@[@3,@4]};
NSDictionary*dic = [mod1 mj_keyValues];
NSLog(@"%@",dic);
这里用到多个接口.h
3.字典转模型 mode是一个 (见上图)
NSDictionary *dict = @{
@"statuses":@[
@{
@"text":@"Nice weather!",
@"user":@{
@"name":@"Rose",
@"icon":@"nami.png"
}
},
@{
@"text":@"Go camping tomorrow!",
@"user":@{
@"name":@"Jack",
@"icon":@"lufy.png"
}
}
],
@"ads":@[
@{
@"image":@"ad01.png",
@"url":@"http://www.ad01.com"
},
@{
@"image":@"ad02.png",
@"url":@"http://www.ad02.com"
}
],
@"totalNumber":@"2014"
};
//字典-》模型
resoponsMode*resopnsM = [resoponsMode mj_objectWithKeyValues:dict];
//字典数组-》模型数组
//NSMutableArray 和NSArray都行
NSMutableArray *userArray = [resoponsMode mj_objectArrayWithKeyValuesArray:resopnsM.statuses];
resoponsMode*resopnsM2 = userArray[0];
NSLog(@"------------%@----%@--%@",resopnsM.totalNumber,resopnsM2.text,resopnsM2.user);
//
resoponsMode*resopnsM3 = [resoponsMode mj_objectWithKeyValues:resopnsM2.user];
NSLog(@"------------%@",resopnsM3.name);
4.字典转模型 返回多个mode
NSDictionary*dict =@{
@"statuses":@[
@{
@"text":@"Nice weather!",
@"user":@[
@{
@"name":@"Rose",
@"icon":@"nami.png"
},
@{
@"name":@"2RoseSecond",
@"icon":@"2Secondnami.png"
}
]
},
@{
@"text":@"Go camping tomorrow!",
@"user":@[
@{
@"name":@"3Rose",
@"icon":@"3nami.png"
},
@{
@"name":@"4RoseSecond",
@"icon":@"4Secondnami.png"
}
]
}
],
@"ads":@[
@{
@"image":@"ad01.png",
@"url":@"http://www.ad01.com"
},
@{
@"image":@"ad02.png",
@"url":@"http://www.ad02.com"
}
],
@"totalNumber":@"2014"
};
//字典-》模型
MoreModel*MoreResopnsM = [MoreModel mj_objectWithKeyValues:dict];
//字典数组-》模型数组
//NSMutableArray 和NSArray都行
NSMutableArray*userArray = [SecondModel mj_objectArrayWithKeyValuesArray:MoreResopnsM.statuses];
SecondModel*secondModel = userArray[0];
NSLog(@"------------%@----%@--%@",MoreResopnsM.totalNumber,secondModel.text,secondModel.user);
NSArray*userArray2 = [ThreeModel mj_objectArrayWithKeyValuesArray:secondModel.user];
ThreeModel*threeModel = userArray2[0];
NSLog(@"------------%@",threeModel.name);
改别名 后台有id new等
mod .h
@property (nonatomic, assign) NSUInteger tid;
mod.m
+ (NSDictionary *)replacedKeyFromPropertyName {
return @{ @"tid":@"id"1011 };
}