美文网首页
MJExtension简单介绍及使用

MJExtension简单介绍及使用

作者: 艳晓 | 来源:发表于2017-02-10 14:16 被阅读251次

MJExtension是一套字典和模型之间互相转换的超轻量级框架

1、字典(JSON) --> 模型(Model)、CoreData模型(Core Data Model)
2、JSON字符串 --> 模型(Model)、CoreData模型(Core Data Model)
3、字典数组(JSON Array) --> 模型数组(Model Array)、Core Data模型数组(Core Data Model Array)
4、JSON字符串 --> 模型数组(Model Array)、Core Data模型数组(Core Data Model Array)
5、模型(Model)、CoreData模型(Core Data Model) --> 字典(JSON)
6、模型数组(Model Array)、Core Data模型数组(Core Data Model Array) --> 字典数组(JSON Array)
7、只需要一行代码,就能实现模型的所有属性进行Coding(归档和解档)

1 2 3 4 5 6是字典与模型之间的相互转换。7是模型属性的归档和解档。
1 2 3 4是JSON格式转换成模型,通常我们用于网络获取数据后,进行数据解析使用。
5 6 为模型转换成JSON格式,上传数据会用到。
7 一句话将所有的子文件全部归档反归档(MJExtension) ,所有Model都会用到,所以写在基类就好。

<code>MJCodingImplementation</code>

一、常用方法——Model中

1.1
<code>+ (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray</code>
该方法在model中用于说明 数组属性,该属性数组中存放那种类型的model,写在init方法内部
<pre>- (instancetype)init
{
self = [super init];
if (self) {
// ZYXStatusResult类中的两个数组中存放的是哪两个模型
[ZYXStatusResult mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"statuses" : @"ZYXStatus", // @"statuses" : [ZYXStatus class],
@"ads" : @"ZYXAd" // @"ads" : [ZYXAd class]
};
}];
}
return self;
}</pre>
1.2

      <code>+ (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName</code>

该方法在model中用于处理属性名和JSON中key值不同的情况,将属性名和key进行对应并替换。写在init方法内部
<pre>

  • (instancetype)init
    {
    self = [super init];
    if (self) {
    [ZYXStudent mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
    return @{
    @"ID" : @"id",
    @"desc" : @"desciption",
    @"oldName" : @"name.oldName",
    @"nowName" : @"name.newName",
    @"info":@"name.info",
    @"nameChangedTime" : @"name.info[1].nameChangedTime",
    @"bag" : @"other.bag"
    };
    }];
    }
    return self;
    }</pre>

二、常用方法——将JSON格式转Model

2.1
<code>+ (instancetype)mj_objectWithKeyValues:(id)keyValues</code>
字典转模型,复合字典转模型,嵌套(数组)字典转模型 JSON字符串转模型都会用到这个方法。以下仅仅是一个事例,本Demo可以通过文字末尾链接查看。
<pre>
// 3、多层字典嵌套转成嵌套模型
-(void)DictionaryTransferToMultiModel{
NSDictionary *dict = @{
@"text" : @"是啊,今天天气确实不错!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@"retweetedStatus" : @{
@"text" : @"今天天气真不错!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}
};
// 将字典转为Status模型
ZYXStatus *status = [ZYXStatus mj_objectWithKeyValues:dict];
NSLog(@"text=%@, name=%@, icon=%@", status.text, status.user.name, status.user.icon);
// text=是啊,今天天气确实不错!, name=Jack, icon=lufy.png

// NSLog(@"text2=%@, name2=%@, icon2=%@", status.retweetedStatus.text, status.retweetedStatus.user.name, status.retweetedStatus.user.icon);
// text2=今天天气真不错!, name2=Rose, icon2=nami.png
}</pre>
2.2
<code>+ (NSMutableArray *)mj_objectArrayWithKeyValuesArray:(NSArray *)keyValuesArray</code>
JSON数组转成模型数组。这个也是非常常用的。
<pre>
-(void)JSONarrayTransferToModelarray{
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
];

// JSON array -> User array
NSArray *userArray = [ZYXUser mj_objectArrayWithKeyValuesArray:dictArray];

// Printing
for (ZYXUser *user in userArray) {
    NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
// name=Jack, icon=lufy.png
// name=Rose, icon=nami.png
ZYXUser *user = userArray[0];
NSLog(@"user.name=%@",user.name);
// user.name=Jack

}
</pre>

三、常用方法——将Model转JSON

3.1
<code>- (NSMutableDictionary *)mj_keyValues</code>
<pre>
-(void)ModelTransferToJSONDictionary{
ZYXUser *user = [[ZYXUser alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";

ZYXStatus *status = [[ZYXStatus alloc] init];
status.user = user;
status.text = @"Nice mood!";

// Status -> JSON
NSDictionary *statusDict = status.mj_keyValues;
NSLog(@"%@", statusDict);

/*
 2017-02-09 10:50:03.372 MJExtensionDemo[866:219284] {
    text = "Nice mood!";
    user =     {
         age = 0;
         gay = 0;
         icon = "lufy.png";
         name = Jack;
         sex = 0;
    };
 }

 */

// More complex situation
ZYXStudent *stu = [[ZYXStudent alloc] init];
stu.ID = @"123";
stu.oldName = @"rose";
stu.nowName = @"jack";
stu.desc = @"handsome";
stu.nameChangedTime = @"2018-09-08";

ZYXBag *bag = [[ZYXBag alloc] init];
bag.name = @"a red bag";
bag.price = 205;
stu.bag = bag;

NSDictionary *stuDict = stu.mj_keyValues;
NSLog(@"%@", stuDict);
/*
 {
     ID = 123;
     bag =     {
          name = "\U5c0f\U4e66\U5305";
          price = 205;
     };
     desc = handsome;
     nameChangedTime = "2018-09-08";
     nowName = jack;
    oldName = rose;
 }
 */

}
</pre>

3.2
<code>+ (NSMutableArray *)mj_keyValuesArrayWithObjectArray:(NSArray *)objectArray</code>
<pre> // 1.新建模型数组
MJUser *user1 = [[MJUser alloc] init];
user1.name = @"Jack";
user1.icon = @"lufy.png";

MJUser *user2 = [[MJUser alloc] init];
user2.name = @"Rose";
user2.icon = @"nami.png";

NSArray *userArray = @[user1, user2];

// 2.将模型数组转为字典数组
NSArray *dictArray = [MJUser mj_keyValuesArrayWithObjectArray:userArray];
MJExtensionLog(@"%@", dictArray);

</pre>

简单使用小Demo:
https://github.com/zhangyanxiao/MJExtensionDemo
MJ:
https://github.com/CoderMJLee/MJExtension

相关文章

  • MJExtension简单介绍及使用

    MJExtension是一套字典和模型之间互相转换的超轻量级框架 1、字典(JSON) --> 模型(Model)...

  • 解读MJExtension

    介绍 关于MJExtension的介绍、使用官方已经介绍的很清楚了,请前往Github MJExtension. ...

  • 无标题文章

    iOS 缓存 快速集成环信 技术文档 Masonry简单使用 MJExtension简单使用 GCD基本使用 gi...

  • MJExtension简介及使用

    一, 简介 1.MJExtension是一套字典和模型之间互相转换的超轻量级框架,支持以下类型转换: JSON –...

  • iOS MJExtension简单使用

    写在前面:因为懒,不爱写东西了。感谢我的兄弟兼iOS引路人韩韩,一直监督着我学习,逼着我理解技术和写出来,因此有了...

  • iOS MJExtension 映射扩展 model转换

    本次将不仔细介绍MJExtension的使用了,关系到映射记一下 MJExtension第三方框架 我们在iOS开...

  • MJExtension解析使用详细介绍

    免责声明:写这篇笔记纯属是方便自己以及能使用到的小伙伴们作为参考,方便查找。无任何利益关系。本文参考使用说明地址链...

  • iOS | MJExtension 介绍 & 源码解析

    介绍: MJExtension是一个转换速度快、使用简单方便的字典转模型框架, 由国内李明杰大神所著, 并且开源...

  • Block 介绍及简单使用

    多谢两位大佬的分享:(传送入口)http://www.jianshu.com/p/51d04b7639f1[htt...

  • Kotlin介绍及简单使用

    Kotlin教程 www.runoob.com/kotlin/kotlin-tutorial.html 中文文档 ...

网友评论

      本文标题:MJExtension简单介绍及使用

      本文链接:https://www.haomeiwen.com/subject/jdkwittx.html