JSONModel

Magical Data Modelling Framework for JSON

https://github.com/icanzilb/JSONModel

New: In version 0.12.0 I added experimental support for exportingJSON modelstoCoreData.

最新消息:在0.12.0版本中,我試驗性的支持將 JSON models 轉(zhuǎn)化成 CoreData .

Give it a try and let me know, post an issue or just get in touch. Try something like that:

如果你試驗過了丘跌,有空告知哥一下秧廉,哥寫開源庫也不容易,發(fā)一篇博文或者給個鏈接以表支持:

NSError*error=nil;GitHubRepoEntity*entity=[GitHubRepoEntityentityWithModel:modelinContext:self.managedObjectContexterror:&error];[self.managedObjectContextsave:nil];

If you like JSONModel and use it can you please: 1) star this repo 2)send me some feedback. Thanks!

JSONModel is a library, which allows rapid creation of smart data models. You can use it in your iOS or OSX apps.

JSONModel automatically introspects your model classes and the structure of your JSON input and reduces drastically the amount of code you have to write.

如果你喜歡 JSONModel ,那么你可以:1)長期關(guān)注這個開源項目,2)你是土豪的話,給哥捐點吧,謝謝.

JSONModel 是一個庫,他能智能并且快速的創(chuàng)建出數(shù)據(jù) model,你可以在你的 iOS 項目或者 OSX 項目上使用它.

Adding JSONModel to your project

添加 JSONModel 到你的工程中

Requirements

需要的環(huán)境:

ARC only; iOS 5.0+ / OSX 10.7+

SystemConfiguration.framework

ARC,iOS 5.0+ / OSX 10.7 +

引入框架SystemConfiguration.framework

Get it as: 1) source files

Download the JSONModel repository as azip fileor clone it

Copy the JSONModel sub-folder into your Xcode project

Link your app to SystemConfiguration.framework

1. ?下載?JSONModel zip包

2. ?將?JSONModel?文件夾拷貝到你的工程項目中

3. ?將庫?SystemConfiguration.framework 添加上

or 2) via Cocoa pods

In your project'sPodfileadd the JSONModel pod:

使用 Cocoa pods 來安裝:

pod'JSONModel'

If you want to read more about CocoaPods, have a look atthis short tutorial.

如果你不會用 CocoaPods雅采,你可以看看這簡單的教程蛛碌。

Source code documentation

源碼的文檔

The source code includes class docs, which you can build yourself and import into Xcode:

源碼本身包含了類的文檔打颤,你可以自己編譯后導(dǎo)入到你的Xcode中:

If you don't already haveappledocinstalled, install it withhomebrewby typingbrew install appledoc.

Install the documentation into Xcode by typingappledoc .in the root directory of the repository.

Restart Xcode if it's already running.

1. ?如果你還沒安裝 appledoc ,先安裝 appledoc

2. ?在Xcode上鍵入 appledoc 安裝文檔苦掘,在根目錄下

3. ?重啟Xcode

Basic usage

基本使用

Consider you have a JSON like this:

假設(shè)你的 JSON 串像下面這樣子:

{"id":"10","country":"Germany","dialCode":49,"isInEurope":true}

Create a new Objective-C class for your data model and make it inherit the JSONModel class.

Declare properties in your header file with the name of the JSON keys:

創(chuàng)建一個你自己的類,并繼承至 JSONModel

在你的頭文件里面進(jìn)行聲明你所需要的 JSON key值

#import "JSONModel.h"@interfaceCountryModel:JSONModel@property(assign,nonatomic)intid;@property(strong,nonatomic)NSString*country;@property(strong,nonatomic)NSString*dialCode;@property(assign,nonatomic)BOOLisInEurope;@end

There's no need to do anything in the.mfile.

.m文件中你不需要做其他的事情了.

Initialize your model with data:

初始化你的 model ,如下所示:

#import "CountryModel.h"...NSString*json=(fetchhereJSONfromInternet)...NSError*err=nil;CountryModel*country=[[CountryModelalloc]initWithString:jsonerror:&err];

If the validation of the JSON passes you have all the corresponding properties in your model populated from the JSON. JSONModel will also try to convert as much data to the types you expect, in the example above it will:

如果傳過來的 JSON 合法,你所定義的所有的屬性都會與該 JSON 值相匹配,并且 JSONModel 也會嘗試盡可能的轉(zhuǎn)換成你所想要的數(shù)據(jù),就像上面的例子:

convert "id" from string (in the JSON) to an int for your class

just copy country's value

convert dialCode from number (in the JSON) to an NSString value

finally convert isInEurope to a BOOL for your BOOL property

轉(zhuǎn)化 "id",從字符串轉(zhuǎn)換成 int 型

拷貝 country 屬性的值

轉(zhuǎn)換 dialCode ,從NSNumber 轉(zhuǎn)換為 NSString 值

最后一個呢是將 isInEurope 轉(zhuǎn)換成 BOOL 的屬性

And the good news is all you had to do is define the properties and their expected types.

所以,你需要做的就是定義出你期望的屬性就行了驮瞧。

Online tutorials

在線教程

Official website:http://www.jsonmodel.com

Class docs online:http://jsonmodel.com/docs/

Step-by-step tutorials:

傻瓜教程:

How to fetch and parse JSON by using data models

Performance optimisation for working with JSON feeds via JSONModel

How to make a YouTube app using MGBox and JSONModel

Examples

例子

Automatic name based mapping

命名自動匹配

{? "id": "123",? "name": "Product name",? "price": 12.95}

@interface ProductModel : JSONModel@property (assign, nonatomic) intid;@property (strong, nonatomic) NSString*name;@property (assign, nonatomic) floatprice;@end@implementation ProductModel@end

Model cascading (models including other models)

model中含有其他的model

{? "order_id": 104,? "total_price": 13.45,? "product" : {? ? "id": "123",? ? "name": "Product name",? ? "price": 12.95? }}

@interface OrderModel : JSONModel@property (assign, nonatomic) intorder_id;@property (assign, nonatomic) floattotal_price;@property (strong, nonatomic)ProductModel*product;@end@implementation OrderModel@end

Model collections

model中含有其他model的集合

{? "order_id": 104,? "total_price": 103.45,? "products" : [? ? {? ? ? "id": "123",? ? ? "name": "Product #1",? ? ? "price": 12.95? ? },? ? {? ? ? "id": "137",? ? ? "name": "Product #2",? ? ? "price": 82.95? ? }? ]}

@protocol ProductModel

@end@interface ProductModel : JSONModel@property (assign, nonatomic) intid;@property (strong, nonatomic) NSString*name;@property (assign, nonatomic) floatprice;@end@implementation ProductModel@end@interface OrderModel : JSONModel@property (assign, nonatomic) intorder_id;@property (assign, nonatomic) floattotal_price;@property (strong, nonatomic)NSArray*products;@end@implementation OrderModel@end

Key mapping

鍵值轉(zhuǎn)回匹配

{? "order_id": 104,? "order_details" : [? ? {? ? ? "name": "Product#1",? ? ? "price": {? ? ? ? "usd": 12.95? ? ? }? ? }? ]}

@interface OrderModel : JSONModel@property (assign, nonatomic) intid;@property (assign, nonatomic) floatprice;@property (strong, nonatomic) NSString*productName;@end@implementation OrderModel+(JSONKeyMapper*)keyMapper{? return [[JSONKeyMapper alloc] initWithDictionary:@{@"order_id": @"id",? ? @"order_details.name": @"productName",? ? @"order_details.price.usd": @"price"}];}@end

Global key mapping (applies to all models in your app)

設(shè)置全局的鍵值轉(zhuǎn)回匹配

[JSONModel setGlobalKeyMapper:[[JSONKeyMapper alloc] initWithDictionary:@{? ? ? @"item_id":@"ID",? ? ? @"item.name": @"itemName"? }]];

Map automatically under_score case to camelCase

將下滑線轉(zhuǎn)換成首字母大寫

{? "order_id": 104,? "order_product" : @"Product#1",? "order_price" : 12.95}

@interface OrderModel : JSONModel@property (assign, nonatomic) intorderId;@property (assign, nonatomic) floatorderPrice;@property (strong, nonatomic) NSString*orderProduct;@end@implementation OrderModel+(JSONKeyMapper*)keyMapper{? return[JSONKeyMappermapperFromUnderscoreCaseToCamelCase];}@end

Optional properties (i.e. can be missing or null)

可以為空的屬性值

{

"id": "123",

"name": null,

"price": 12.95

}@interface ProductModel : JSONModel@property (assign, nonatomic) int id;@property (strong, nonatomic) NSString*name;@property (assign, nonatomic) float price;@property (strong, nonatomic) NSNumber*uuid;@end@implementation ProductModel@end

Ignored properties (i.e. JSONModel completely ignores them)

忽略某些屬性

{

"id": "123",

"name": null

}@interface ProductModel : JSONModel@property (assign, nonatomic) int id;@property (strong, nonatomic) NSString*customProperty;@end@implementation ProductModel@end

Make all model properties optional (avoid if possible)

讓所有的屬性都可以有空的屬性值

@implementation ProductModel+(BOOL)propertyIsOptional:(NSString*)propertyName

{

return YES;

}@end

Lazy convert collection items from dictionaries to models

將集合元素轉(zhuǎn)換成 model

{? "order_id": 104,? "total_price": 103.45,? "products" : [? ? {? ? ? "id": "123",? ? ? "name": "Product #1",? ? ? "price": 12.95? ? },? ? {? ? ? "id": "137",? ? ? "name": "Product #2",? ? ? "price": 82.95? ? }? ]}

@protocol ProductModel@end@interface ProductModel : JSONModel@property (assign, nonatomic) intid;@property (strong, nonatomic) NSString*name;@property (assign, nonatomic) floatprice;@end@implementation ProductModel@end@interface OrderModel : JSONModel@property (assign, nonatomic) int order_id;@property (assign, nonatomic) float total_price;@property (strong, nonatomic) NSArrayConvertOnDemand>* products;@end@implementation OrderModel@end

Using the built-in thin HTTP client

使用內(nèi)置的 HTTP 鏈接

//addextraheaders[[JSONHTTPClientrequestHeaders]setValue:@"MySecret"forKey:@"AuthorizationToken"];//makepost,getrequests[JSONHTTPClientpostJSONFromURLWithString:@"http://mydomain.com/api"params:@{@"postParam1":@"value1"}completion:^(idjson,JSONModelError*err){//checkerr,processjson...}];

Export model to NSDictionary or to JSON text

將 model 導(dǎo)出為字典或者json字符串

ProductModel*pm=[[ProductModelalloc]initWithString:jsonStringerror:nil];pm.name=@"Changed Name";//converttodictionaryNSDictionary*dict=[pmtoDictionary];//converttotextNSString*string=[pmtoJSONString];

json validation

data transformations

error handling

custom data validation

automatic compare and equality features

and more.

json數(shù)據(jù)鍵值匹配

數(shù)據(jù)轉(zhuǎn)換

好的容錯能力

自定義數(shù)據(jù)鍵值匹配

自動比較以及判斷的特性

還有更多的等待親來挖掘

以下是本人使用的測試結(jié)果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鹤啡,一起剝皮案震驚了整個濱河市梗顺,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌赚瘦,老刑警劉巖粟誓,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蚤告,居然都是意外死亡努酸,警方通過查閱死者的電腦和手機(jī)服爷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門杜恰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人仍源,你說我怎么就攤上這事心褐。” “怎么了笼踩?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵逗爹,是天一觀的道長。 經(jīng)常有香客問我嚎于,道長掘而,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任于购,我火速辦了婚禮袍睡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘肋僧。我一直安慰自己斑胜,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布嫌吠。 她就那樣靜靜地躺著止潘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪辫诅。 梳的紋絲不亂的頭發(fā)上凭戴,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機(jī)與錄音炕矮,去河邊找鬼簇宽。 笑死勋篓,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的魏割。 我是一名探鬼主播譬嚣,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼钞它!你這毒婦竟也來了拜银?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤遭垛,失蹤者是張志新(化名)和其女友劉穎尼桶,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锯仪,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡泵督,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了庶喜。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片小腊。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖久窟,靈堂內(nèi)的尸體忽然破棺而出秩冈,到底是詐尸還是另有隱情,我是刑警寧澤斥扛,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布入问,位于F島的核電站,受9級特大地震影響稀颁,放射性物質(zhì)發(fā)生泄漏芬失。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一匾灶、第九天 我趴在偏房一處隱蔽的房頂上張望棱烂。 院中可真熱鬧,春花似錦粘昨、人聲如沸垢啼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽芭析。三九已至,卻和暖如春吞瞪,著一層夾襖步出監(jiān)牢的瞬間馁启,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留惯疙,地道東北人翠勉。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像霉颠,于是被迫代替她去往敵國和親对碌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

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

  • 前言 如今基本都是客戶端請求服務(wù)器的模式蒿偎,數(shù)據(jù)傳遞方式基本都使用JSON的字符串方式朽们。然而當(dāng)我們獲取了服務(wù)器發(fā)來的...
    Alibsolute閱讀 5,193評論 6 4
  • iOS中JSONModel的使用 Adding JSONModel to your project (https:...
    夜宇天閱讀 1,541評論 0 1
  • 準(zhǔn)備: JSONModel_下載 快速的解析數(shù)據(jù)為Model模型,支持層級嵌套Model模型解析,指定類型自動轉(zhuǎn)化...
    sunmumu1222閱讀 1,185評論 0 1
  • MVVM模式與團(tuán)隊合作 說到架構(gòu)設(shè)計和團(tuán)隊協(xié)作诉位,這個對App的開發(fā)還是比較重要的骑脱。即使作為一個專業(yè)的搬磚者,前提是...
    GitHubPorter閱讀 6,670評論 9 19
  • 《大話西游》的結(jié)尾有句話,“你看那個人岳瞭,好奇怪呦拥娄,像一條狗∏抻牛”像條狗的人条舔,是放棄了人間的男女情歡枫耳,無欲無念兢兢業(yè)業(yè)...
    夢槑閱讀 7,409評論 1 7