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
例子
命名自動(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)注“簡書作者”。