代码库> 其他> Objective-C Model、JSON、NSDictionary互相转换简单高效的轻量级框架,支持model嵌套model。
Objective-C Model、JSON、NSDictionary互相转换简单高效的轻量级框架,支持model嵌套model。
关注
上传者:984377925 分类:其他(Others)
查看次数:2287 下载次数:161
上传时间:2015-05-01 大小:26 B
测试环境:Xcode 6.2,iOS 6.0以上


Features

GenericModel 支持Objective-C Model、NSDictionary、JSON之间互相转换,框架非常简单高效,内部对反射过的model设置有缓存,用Objective-C中的Protocol限定NSArray,NSDictionary等容器类的类型,防止容器类型变量类型使用错误,类似Java中容器类型的泛型。
支持的类型转换
NSDictionary <--> Model
JSON <--> Model
Example

JSON -> Model

JSON类型转简单Model


//StudentModel.h
@protocol StudentModel @end
@interface StudentModel : GenericModel

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *hobby;
@property (nonatomic, assign) NSInteger age;

@end
//Example Code
NSString *jsonString = @"{"name":"Name1", "hobby":"Basketball", "age":14}";
StudentModel *studentModel = [GenericModel getObjectByJSON:jsonString clazz:[StudentModel class]];
NSLog(@"studentModel:name:%@, hobby:%@, age:%ld",studentModel.name, studentModel.hobby, (long)studentModel.age);
//Output
2015-05-01 18:43:07.998 GenericModel[16634:359368] studentModel:name:Name1, hobby:Basketball, age:14



Model -> JSON

简单Model转JSON类型


StudentModel *student_1 = [[StudentModel alloc] init];
student_1.name = @"student_1";
student_1.hobby = @"BasketBall";
student_1.age = 15;

StudentModel *student_2 = [[StudentModel alloc] init];
student_2.name = @"student_2";
student_2.hobby = @"Football";
student_2.age = 14;

GradeModel *gradeMode = [[GradeModel alloc] init];
[gradeMode.students addObject:student_1];
[gradeMode.students addObject:student_2];

NSString *resultJSON = [GenericModel getJSONByObject:gradeMode];
NSLog(@"resultJSON:%@",resultJSON);
//Output
resultJSON:{"students":[{"name":"student_1","hobby":"BasketBall","age":15},{"name":"student_2","hobby":"Football","age":14}]}










收藏
我来说两句
发表评论
您还没有登录!请登录注册
所有评论(0
提示
sina weixin mail 回到顶部