分享一个使用runtime进行json转model的方法, 有很多开源库, 比如YYModel, MJExtension等等, 都可用于json解析, 但是我做iOS开发两年的时间里用的都是runtime封装的方法, 觉得很好用, 也没有出现问题.
@interface QSMObject : NSObject
//**
* 传入一个字典, 返回值的当前传入的字典对应的model值
*/
- (id)initWithDic:(NSDictionary *)dic;
//**
* 传入一个数组, 数组对应的是很多歌字典, 返回值的数组对应的每个字典所对应的model数组
*/
+ (NSMutableArray *) getListArray:(NSArray *) aArray;
@end
#import <objc/runtime.h> //引入runtime
@implementation QSMObject
- (id)initWithDic:(NSDictionary *)dic {
if (self = [super init]) {
unsigned int outCount = 0;
Class currentClass = [self class];
while (currentClass) {
//获取当前类所有的属性
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (int i = 0; i < outCount; i++) {
//获取该类的一个属性指针
objc_property_t property = properties[i];;
//property_getName(property) 获取属性的名称
//initWithCString, c的字符串转OC的字符串
NSString *propertyNameString =[[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
//因为id是一个OC的关键字, 你不能声明一个属性名称为id, 所以遇到属性名称是id就携程id_i
//如果属性名称是id_i, 则把id赋值给当前属性
if ([propertyNameString isEqualToString:@"id_i"]) {
propertyNameString = @"id";
}
id value;
//判断传过来的参数是不是字典类型
if ([dic isKindOfClass:[NSDictionary class]]) {
//根据key获取json数据对应的value
//如果对应的可以不存在, 使用字典去获取一个value值, 则返回值为nil
value = [dic objectForKey:propertyNameString];
}
//如果该值不存在
if (value == [NSNull null]|| value == nil) {
//则返回继续寻找
continue;
}
//如果值存在
if (value) {
//但度处理名称为id的
if ([propertyNameString isEqualToString:@"id"]) {
[self setValue:[dic objectForKey:@"id"] forKey:@"id_i"];
}else{
//根据属性, 使用kvc赋值
[self setValue:value forKey:propertyNameString];
}
}
}
//最后别忘记释放
free(properties);
Class superclass = [currentClass superclass];
currentClass = superclass;
}
}
return self;
}
+ (NSMutableArray *) getListArray:(NSArray *) aArray
{
if (aArray == nil) {
return [NSMutableArray array];
}
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for(NSDictionary *dic in aArray)
{
[dataArray addObject:[[self alloc]initWithDic:dic]];
}
return dataArray;
}
+ (NSMutableArray *) getListArray:(NSArray *) aArray
{
if (aArray == nil) {
return [NSMutableArray array];
}
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for(NSDictionary *dic in aArray)
{
[dataArray addObject:[[self alloc]initWithDic:dic]];
}
return dataArray;
}
@end
如何使用
假如从服务器上获取的数据是这样的
/**
school = "xx大学";
group = {
teacher = "王老师";
classNo = 17;
};
personlist= [
{
name = "小名";
sex = "男";
};
{
name = "小丽";
sex = "女";
};
];
*/
首先创建对应的mode类
@class GroupModel, PersonModel;
@interface TheClass : JSObject
@property (nonatomic, strong)NSString *school;
@property (nonatomic, strong)GroupModel *group; //注意此处的类型
@property (nonatomic, strong)NSArray <PersonModel *>*personlist;
@end
@interface GroupModel : JSObject
@property (nonatomic, strong)NSString *teacher;
@property (nonatomic, assign)NSInteger classNo;
@end
@interface PersonModel : JSObject
@property (nonatomic, strong)NSString *name;
@property (nonatomic, strong)NSString *sex;
@end
@implementation TheClass
- (id)initWithDic:(NSDictionary *)dic {
self = [super initWithDic:dic];
if (self) {
if ([dic.allKeys containsObject:@"group"]) {
GroupModel *model = [[GroupModel alloc] initWithDic:dic[@"group"]];
_group = model;
}else {
_group = nil;
}
if ([dic.allKeys containsObject:@"personlist"]) {
NSArray *personlist = [PersonModel getListArray:dic[@"personlist"]];
_personlist = personlist;
}else {
_personlist = nil;
}
}
return self;
}
@end
@implementation GroupModel
@end
@implementation PersonModel
@end
如果你属性写错, 或者少些, 或者多写, 进行json解析的时候都不会引起crash
网友评论