MJExtension转模型初探

项目里经常会遇到字典或者字典包含数组,也许是更复杂的数据结构,一层层剥离,其实也可以,但是借助 MJExtension字典或者数组各种嵌套转模型一行代码再加一些配置即可完成一部解析,清晰明了。

 举个简单的例子介绍,字典里面包含数组,数组里面包含字典,


类似这样,可以初步建个 最外层的model

---------model-------------------------------

@interface Model : NSObject

@property(nonatomic,strong)NSString *success;

@property(nonatomic,strong)NSString *totPrice;

@property(nonatomic,strong)NSArray *OrgBeanList;//必须和图中url 解析的字段保持一致

在字典里嵌套数组的时候,不用引用接下来的类,只需要在.m中实现一个方法:

+ (NSDictionary *)objectClassInArray{

return @{

@"OrgBeanList" : @"ListModel",------数组包含的model

};

}

------ListModel--------

@property(nonatomic,strong)NSArray *customerCourses;//同样字段不能错。.m里要实现引用CustomerCourses的方法;

@property(nonatomic,strong)NSString *subtotal;

@property(nonatomic,strong)NSString *address;

@property(nonatomic,strong)NSString *name;

----CustomerCourses----

@class courseType;

@class Course;

@interface CustomerCourses : NSObject

@property(nonatomic,strong)NSString *organizationId;

@property(nonatomic,strong)NSString *totalPrice;

@property(nonatomic,strong)NSString *amount;

@property(nonatomic,strong)courseType *courseType;//模型嵌套模型,

@property(nonatomic,strong)Course *course;//模型嵌套模型

------courseType---------

@property(nonatomic,strong)NSString *name;

@property(nonatomic,strong)NSString *courseTypeDescription;//因为description和系统的一直,所以需要替换属性名:.m中实现:

+ (NSDictionary *)replacedKeyFromPropertyName{

return @{

@"courseTypeDescription" : @"description",

};

}

@property(nonatomic,strong)NSString *ord;

自己写的小demo地址:MJExtension 字典转模型 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容