MJExtension解析使用詳細介紹

免責聲明:寫這篇筆記純屬是方便自己以及能使用到的小伙伴們作為參考,方便查找。無任何利益關系。

本文參考使用說明地址鏈接: https://github.com/CoderMJLee/MJExtension ? (MJ大牛的解析github鏈接)

MJExtension解析demo

開發(fā)肯定離不開數(shù)據(jù)的解析,有時候后臺的隊友當然也是因為數(shù)據(jù)的需要才不得已搞出那么多層,各種嵌套,以至于應用開發(fā)時解析很蛋疼。我們經(jīng)常用到解析數(shù)據(jù)的類庫(JSON、XML)有JSONModel 、JSONKit(系統(tǒng)自帶iOS 5 后)、MJExtension、GDataXMLNode(需要導入sqlit3)、GDataXML-HTML(直接使用)-------我知道的也是用到過的就這幾個;其中關于JSON類型數(shù)據(jù)的解析效率或者說性能 MJExtension > JSONModel :

要求所有模型類必須繼承自JSONModel基類;

不需要你的模型類繼承任何特殊基類,毫無污染,毫無侵入性;

MJExtension是一套字典和模型之間互相轉(zhuǎn)換的超輕量級框架

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.

只需要一行代碼,就能實現(xiàn)模型的所有屬性進行Coding(歸檔和解檔)

簡單的字典 --> 模型

JSON字符串 --> 模型

復雜的字典 --> 模型 (模型里面包含了模型)

復雜的字典 --> 模型 (模型的數(shù)組屬性里面又裝著模型)

復雜的字典 --> 模型(模型屬性名和字典的key不一樣)

字典數(shù)組? -->? 模型數(shù)組

模型 --> 字典

模型數(shù)組 --> 字典數(shù)組

字典 --> CoreData模型

歸檔與解檔NSCoding

過濾字典的值

使用方法我就不廢話了,你直接拖文件或者cocoapods導入,自己看著弄;然后記得導入頭文件就行: #import "MJExtension.h"

開始放大招了:

一 、最簡單的字典轉(zhuǎn)換為模型

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

三、模型中有個數(shù)組,數(shù)組中有模型

@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開發(fā)",

@"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解析使用詳解;

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內(nèi)容