免責(zé)聲明:寫這篇筆記純屬是方便自己以及能使用到的小伙伴們作為參考使套,方便查找玉罐。無任何利益關(guān)系绸吸。
本文參考使用說明地址鏈接: https://github.com/CoderMJLee/MJExtension ? (MJ大牛的解析github鏈接)
開發(fā)肯定離不開數(shù)據(jù)的解析扫茅,有時候后臺的隊友當(dāng)然也是因為數(shù)據(jù)的需要才不得已搞出那么多層,各種嵌套芽隆,以至于應(yīng)用開發(fā)時解析很蛋疼。我們經(jīng)常用到解析數(shù)據(jù)的類庫(JSON、XML)有JSONModel 胚吁、JSONKit(系統(tǒng)自帶iOS 5 后)牙躺、MJExtension、GDataXMLNode(需要導(dǎo)入sqlit3)腕扶、GDataXML-HTML(直接使用)-------我知道的也是用到過的就這幾個孽拷;其中關(guān)于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字符串 --> 模型
復(fù)雜的字典 --> 模型 (模型里面包含了模型)
復(fù)雜的字典 --> 模型 (模型的數(shù)組屬性里面又裝著模型)
復(fù)雜的字典 --> 模型(模型屬性名和字典的key不一樣)
字典數(shù)組? -->? 模型數(shù)組
模型 --> 字典
模型數(shù)組 --> 字典數(shù)組
字典 --> CoreData模型
歸檔與解檔NSCoding
過濾字典的值
使用方法我就不廢話了环形,你直接拖文件或者cocoapods導(dǎo)入,自己看著弄衙傀;然后記得導(dǎo)入頭文件就行: #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解析使用詳解擎析;