MJExtension解析使用详细介绍

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

本文参考使用说明地址链接: https://github.com/CoderMJLee/MJExtension   (MJ大牛的解析github链接)

MJExtension解析demo

开发肯定离不开数据的解析,有时候后台的队友当然也是因为数据的需要才不得已搞出那么多层,各种嵌套,以至于应用开发时解析很蛋疼。我们经常用到解析数据的类库(JSON、XML)有JSONModel 、JSONKit(系统自带iOS 5 后)、MJExtension、GDataXMLNode(需要导入sqlit3)、GDataXML-HTML(直接使用)-------我知道的也是用到过的就这几个;其中关于JSON类型数据的解析效率或者说性能 MJExtension > JSONModel :

要求所有模型类必须继承自JSONModel基类;

不需要你的模型类继承任何特殊基类,毫无污染,毫无侵入性;

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

JSON-->Model、Core Data Model

JSONString-->Model、Core Data Model

Model、Core Data Model-->JSON

JSON Array-->Model Array、Core Data Model Array

JSONString-->Model Array、Core Data Model Array

Model Array、Core Data Model Array-->JSON Array

Coding all properties of model in one line code.

只需要一行代码,就能实现模型的所有属性进行Coding(归档和解档)

简单的字典 --> 模型

JSON字符串 --> 模型

复杂的字典 --> 模型 (模型里面包含了模型)

复杂的字典 --> 模型 (模型的数组属性里面又装着模型)

复杂的字典 --> 模型(模型属性名和字典的key不一样)

字典数组  -->  模型数组

模型 --> 字典

模型数组 --> 字典数组

字典 --> CoreData模型

归档与解档NSCoding

过滤字典的值

使用方法我就不废话了,你直接拖文件或者cocoapods导入,自己看着弄;然后记得导入头文件就行: #import "MJExtension.h"

开始放大招了:

一 、最简单的字典转换为模型

typedef enum {

SexMale,

SexFemale

} Sex;

@interface User : NSObject

@property (copy, nonatomic) NSString *name;

@property (copy, nonatomic) NSString *icon;

@property (assign, nonatomic) unsigned int age;

@property (copy, nonatomic) NSString *height;

@property (strong, nonatomic) NSNumber *money;

@property (assign, nonatomic) Sex sex;

@property (assign, nonatomic, getter=isGay) BOOL gay;

@end

/***********************************************/

NSDictionary *dict = @{

@"name" : @"Jack",

@"icon" : @"lufy.png",

@"age" : @20,

@"height" : @"1.55",

@"money" : @100.9,

@"sex" : @(SexFemale),

@"gay" : @"true"

//  @"gay" : @"1"

//  @"gay" : @"NO"

};

// JSON -> User

User *user = [User mj_objectWithKeyValues:dict];

NSLog(@"name=%@, icon=%@, age=%zd, height=%@, money=%@, sex=%d, gay=%d", user.name, user.icon, user.age, user.height, user.money, user.sex, user.gay);

// name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1

二、模型中嵌套模型

@interface Status : NSObject

@property (copy, nonatomic) NSString *text;

@property (strong, nonatomic) User *user;

@property (strong, nonatomic) Status *retweetedStatus;

@end

/***********************************************/

NSDictionary *dict = @{

@"text" : @"Agree!Nice weather!",

@"user" : @{

@"name" : @"Jack",

@"icon" : @"lufy.png"

},

@"retweetedStatus" : @{

@"text" : @"Nice weather!",

@"user" : @{

@"name" : @"Rose",

@"icon" : @"nami.png"

}

}

};

// JSON -> Status

Status *status = [Status mj_objectWithKeyValues:dict];

NSString *text = status.text;

NSString *name = status.user.name;

NSString *icon = status.user.icon;

NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);

// text=Agree!Nice weather!, name=Jack, icon=lufy.png

NSString *text2 = status.retweetedStatus.text;

NSString *name2 = status.retweetedStatus.user.name;

NSString *icon2 = status.retweetedStatus.user.icon;

NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);

// text2=Nice weather!, name2=Rose, icon2=nami.png

三、模型中有个数组,数组中有模型

@interface Ad : NSObject

@property (copy, nonatomic) NSString *image;

@property (copy, nonatomic) NSString *url;

@end

@interface StatusResult : NSObject

/** Contatins status model */

@property (strong, nonatomic) NSMutableArray *statuses;

/** Contatins ad model */

@property (strong, nonatomic) NSArray *ads;

@property (strong, nonatomic) NSNumber *totalNumber;

@end

/***********************************************/

// Tell MJExtension what type model will be contained in statuses and ads.

[StatusResult mj_setupObjectClassInArray:^NSDictionary *{

return @{

@"statuses" : @"Status",

// @"statuses" : [Status class],

@"ads" : @"Ad"

// @"ads" : [Ad class]

};

}];

// Equals: StatusResult.m implements +mj_objectClassInArray method.

NSDictionary *dict = @{

@"statuses" : @[

@{

@"text" : @"Nice weather!",

@"user" : @{

@"name" : @"Rose",

@"icon" : @"nami.png"

}

},

@{

@"text" : @"Go camping tomorrow!",

@"user" : @{

@"name" : @"Jack",

@"icon" : @"lufy.png"

}

}

],

@"ads" : @[

@{

@"image" : @"ad01.png",

@"url" : @"http://www.ad01.com"

},

@{

@"image" : @"ad02.png",

@"url" : @"http://www.ad02.com"

}

],

@"totalNumber" : @"2014"

};

// JSON -> StatusResult

StatusResult *result = [StatusResult mj_objectWithKeyValues:dict];

NSLog(@"totalNumber=%@", result.totalNumber);

// totalNumber=2014

// Printing

for (Status *status in result.statuses) {

NSString *text = status.text;

NSString *name = status.user.name;

NSString *icon = status.user.icon;

NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);

}

// text=Nice weather!, name=Rose, icon=nami.png

// text=Go camping tomorrow!, name=Jack, icon=lufy.png

// Printing

for (Ad *ad in result.ads) {

NSLog(@"image=%@, url=%@", ad.image, ad.url);

}

// image=ad01.png, url=http://www.ad01.com

// image=ad02.png, url=http://www.ad02.com

四、归档、解档

#import "MJExtension.h"

@implementation Bag

// NSCoding Implementation

MJExtensionCodingImplementation

@end

/***********************************************/

// what properties not to be coded

[Bag mj_setupIgnoredCodingPropertyNames:^NSArray *{

return @[@"name"];

}];

// Equals: Bag.m implements +mj_ignoredCodingPropertyNames method.

// Create model

Bag *bag = [[Bag alloc] init];

bag.name = @"Red bag";

bag.price = 200.8;

NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/bag.data"];

// Encoding

[NSKeyedArchiver archiveRootObject:bag toFile:file];

// Decoding

Bag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];

NSLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price);

// name=(null), price=200.800000

五、过滤字典的值

// Book

#import "MJExtension.h"

@implementation Book

- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property

{

if ([property.name isEqualToString:@"publisher"]) {

if (oldValue == nil) return @"";

} else if (property.type.typeClass == [NSDate class]) {

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];

fmt.dateFormat = @"yyyy-MM-dd";

return [fmt dateFromString:oldValue];

}

return oldValue;

}

@end

// NSDictionary

NSDictionary *dict = @{

@"name" : @"5分钟突破iOS开发",

@"publishedTime" : @"2011-09-10"

};

// NSDictionary -> Book

Book *book = [Book mj_objectWithKeyValues:dict];

// printing

NSLog(@"name=%@, publisher=%@, publishedTime=%@", book.name, book.publisher, book.publishedTime);

还有好多用法我没有摘过来,想要具体的了解建议去MJ的github直接参考。MJExtension解析使用详解;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,961评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,444评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,009评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,082评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,101评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,271评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,738评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,395评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,539评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,434评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,481评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,160评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,749评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,816评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,038评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,548评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,140评论 2 341

推荐阅读更多精彩内容