iOS中JSONModel的使用

iOS中JSONModel的使用

Adding JSONModel to your project (https://github.com/icanzilb/JSONModel)

添加JSONModel到你的項(xiàng)目中

Requirements

需要的環(huán)境

ARC only; iOS 5.0+ / OSX 10.7+

SystemConfiguration.framework(需要導(dǎo)入系統(tǒng)庫)

Get it as: 1) source files

Download the JSONModel repository as azip fileor clone it

下載JSONModel.zip文件

Copy the JSONModel sub-folder into your Xcode project

將它拷貝到你的項(xiàng)目中

Link your app to SystemConfiguration.framework

導(dǎo)入SystemConfiguration.framework框架

or 2) via Cocoa pods

使用Cocoa pods引入

In your project'sPodfileadd the JSONModel pod:

pod 'JSONModel'

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

如果你想關(guān)于CocoaPods了解更多纹冤,請參考這個(gè)簡單的教程

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 typing brew install appledoc.

如果你還沒有安裝appledoc,先安裝appledoc

Install the documentation into Xcode by typing appledoc .

in the root directory of the repository.

在xcode上安裝appledoc文檔昂秃,在根目錄下

Restart Xcode if it's already running.

重啟xcode

Basic usage

基本使用

Consider you have a JSON like this:

假如你的JSON數(shù)據(jù)像這樣:

{"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.

創(chuàng)建一個(gè)Objective-C類朱巨,繼承自JSONModel

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

將JSON中的keys在.h文件中聲明為屬性

#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 .m file.

在.m文件中不需要做任何事情

Initialize your model with data:

用數(shù)據(jù)初始化你的model

#import"CountryModel.h"...NSString* json = (fetch here JSON from Internet) ...NSError* err =nil;CountryModel* country = [[CountryModel alloc] initWithString:json? ? error:&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合法他嫡,你所定義的所有的屬性都會(huì)與該JSON的值想對應(yīng),甚至JSONModel會(huì)嘗試去轉(zhuǎn)換數(shù)據(jù)為你期望的類型备籽,如上所示:

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

just copy country's value

轉(zhuǎn)換id舶治,從字符串轉(zhuǎn)換為int

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

轉(zhuǎn)換diaCode分井,從number轉(zhuǎn)換為字符串

finally convert isInEurope to a BOOL for your BOOL property

最后一個(gè)是將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

命名自動(dòng)匹配

{

"id": "123",

"name": "Product name",

"price": 12.95

}

@interfaceProductModel:JSONModel@property(assign,nonatomic)intid;@property(strong,nonatomic)NSString* name;@property(assign,nonatomic)floatprice;@end@implementationProductModel@end

文/jueyingxx(簡書作者)

原文鏈接:http://www.reibang.com/p/3cce56f374b4

著作權(quán)歸作者所有霉猛,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)尺锚,并標(biāo)注“簡書作者”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末惜浅,一起剝皮案震驚了整個(gè)濱河市瘫辩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌坛悉,老刑警劉巖伐厌,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異裸影,居然都是意外死亡挣轨,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門轩猩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卷扮,“玉大人,你說我怎么就攤上這事均践∥钋拢” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵彤委,是天一觀的道長鞭铆。 經(jīng)常有香客問我,道長焦影,這世上最難降的妖魔是什么衔彻? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮偷办,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘澄港。我一直安慰自己椒涯,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布回梧。 她就那樣靜靜地躺著废岂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪狱意。 梳的紋絲不亂的頭發(fā)上湖苞,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天,我揣著相機(jī)與錄音详囤,去河邊找鬼财骨。 笑死镐作,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的隆箩。 我是一名探鬼主播该贾,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼捌臊!你這毒婦竟也來了杨蛋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤理澎,失蹤者是張志新(化名)和其女友劉穎逞力,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糠爬,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡寇荧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了秩铆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片砚亭。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖殴玛,靈堂內(nèi)的尸體忽然破棺而出捅膘,到底是詐尸還是另有隱情胎挎,我是刑警寧澤猩系,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站昧穿,受9級特大地震影響凡壤,放射性物質(zhì)發(fā)生泄漏署尤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一亚侠、第九天 我趴在偏房一處隱蔽的房頂上張望曹体。 院中可真熱鬧,春花似錦硝烂、人聲如沸箕别。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽串稀。三九已至,卻和暖如春狮杨,著一層夾襖步出監(jiān)牢的瞬間母截,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工橄教, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留清寇,地道東北人喘漏。 一個(gè)月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像颗管,于是被迫代替她去往敵國和親陷遮。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

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

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,478評論 5 6
  • Magical Data Modelling Framework for JSON https://github....
    橙娃閱讀 886評論 1 0
  • MVVM模式與團(tuán)隊(duì)合作 說到架構(gòu)設(shè)計(jì)和團(tuán)隊(duì)協(xié)作垦江,這個(gè)對App的開發(fā)還是比較重要的帽馋。即使作為一個(gè)專業(yè)的搬磚者,前提是...
    GitHubPorter閱讀 6,663評論 9 19
  • 這是那氨瓤浴绽族?李洋揉了揉太陽穴,慢慢撐起身子衩藤,打量四周吧慢,只見四周一片灰暗,伸手不見五指赏表,一陣悠悠的古曲從遠(yuǎn)方陣陣傳...
    左手淘氣閱讀 251評論 1 2
  • 2016.06.01 今天是喵上幼兒園后的第二個(gè)六一兒童節(jié)了检诗,早上七點(diǎn)半就要吃完飯去幼兒園。 一早上起來瓢剿,因?yàn)榉且?..
    摹喵居士閱讀 254評論 0 0