不知道MJExtension什么时候升到 2.0 了然后出现了一些问题,经过一番法研究终于搞定了
//重要的就是这句要怎么做(保留的模型)
1.其中一种声明方法:(使用方法:直接在网络请求之后获得的字典之后)
//此处的 data必须是网络请求之后的字典(因为AFN默默地帮我们转好了中的关键词
//不然的话转换出来的东西就为空
//此处的CFData必须是要保存的模型类型,要声明class
[CFNotesResultmj_setupObjectClassInArray:^NSDictionary *{
return @{@"data" : @"CFData"};}];
2.另一种实现保留模型的方法:(使用方法:在模型的.m文件中实现下面方法)
+ (NSDictionary *)mj_objectClassInArray
{
return @{@"data" : @"CFData"};
}
//此处是对整个 返回的json结果进行模型转换
// 此处直接从result用.语法就可以取到属性data(其中放的是模型数组)
#import <Foundation/Foundation.h> /** * 项目列表 */ @class Project; @interface Projects : NSObject #warning 此处的 projects为返回的字典中的关键字 @property (nonatomic,strong) NSMutableArray *projects; @end @interface Project : NSObject <NSCopying,NSMutableCopying> @property (nonatomic,copy)NSString *addrs; @property (nonatomic,copy)NSString *all_price; @property (nonatomic,copy)NSString *cpai_num; @property (nonatomic,copy)NSString *created; @property (nonatomic,copy)NSString *dtime; @property (nonatomic,copy)NSString *fp_time; /** * 自己是否关注该项目 收藏状态 */ @property (nonatomic,assign) int hasliked; @property (nonatomic,copy)NSString *ID; @property (nonatomic,copy)NSString *jianmianji; @property (nonatomic,copy)NSString *level; /** * 多少人喜欢 */ @property (nonatomic,copy)NSString *liked; @property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *score; @property (nonatomic,copy)NSString *sellmianji; @property (nonatomic,copy)NSString *status; @property (nonatomic,copy)NSString *vote_num; @property (nonatomic,copy)NSString *jindu; @property (nonatomic,copy)NSString *jindu_level; /** * po result { data = { projects = ( { addrs = "\U91ce\U751f\U52a8\"; "all_price" = 9000; "cpai_num" = 0; created = 2014; dtime = 0; "fp_time" = 1448193215; id = 1; jianmianji = 72000; level = 4; liked = 0; score = 30; sellmianji = 0; status = 1; "vote_num" = 0; } ); }; info = ""; status = 1; } */ @end
#import "Projects.h" @implementation Projects +(NSDictionary *)mj_objectClassInArray { return @{ @"projects" : @"Project" #warning 此处的 projects 为字典中 的关键字 Project为自己想要保存的模型 }; } /** // JSON array -> User array NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray]; */ @end @implementation Project + (NSDictionary *)mj_replacedKeyFromPropertyName { return @{ @"ID" : @"id" }; #warning 此处的ID 为替代 返回的字典中的关键字id } @end
// 此处直接就获得 模型数组
self.projectsArr = [Project mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
//或者
Projects *projects = [Projects mj_objectWithKeyValues:result];
//然后直接就能 projects.projects 获取到模型数组
老掉牙的话:字典转模型是不是有必要 要看自己的使用习惯 ,有人用惯了 使用字典的[@""] ,有人喜欢使用模型的.