Https://github.com/icanzilb/JSONModel
New: In version 0.12.0 I added experimental support for exporting JSON models to CoreData.
Latest news: In version 0.12.0, my experimental support translated JSON models into CoreData.
Give it a try and let me know, post an issue or just get in touch. Try something like that:
If you have tried, have time to tell the elder brother, brother write open Source Library is not easy, send a blog post or give a link to the table support:
Nserrornil; Githubrepoentity[entitywithmodel:incontext:self. Error:&error]; [self. SaveNil];
If you like Jsonmodel and use it can 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 AM Ount of code you has to write.
If you like Jsonmodel, then you can: 1) long-term attention to this open source project, 2) you are local tyrants words, give brother donate point, thank you.
Jsonmodel is a library that can intelligently and quickly create a data model that you can use on your IOS project or OSX project.
Adding Jsonmodel to your project add Jsonmodel to your project requirements
Required Environment:
- ARC only; IOS 5.0+/OSX 10.7+
- Systemconfiguration.framework
- Arc,ios 5.0+/OSX 10.7 +
- Introducing Frame Systemconfiguration.framework
Get it as:1) source files
- Download the Jsonmodel repository as a zip file or clone it
- Copy the Jsonmodel sub-folder into your Xcode project
- Link your app to Systemconfiguration.framework
1. Download Jsonmodel Zip Package
2. Copy the Jsonmodel folder to your project
3. Add the Library Systemconfiguration.framework
or 2) via Cocoa pods
In your project ' s podfile Add the Jsonmodel pod:
To install using Cocoa pods:
' Jsonmodel '
If you want to read more about CocoaPods, there is a look in this short tutorial.
If you don't use CocoaPods, you can take a look at this simple tutorial.
Source Code Documentation
Documentation for the source code
The source code includes class docs, which you can build yourself and import into Xcode:
The source itself contains the documentation for the class, which you can compile yourself and import into your Xcode:
- If you don ' t already has Appledoc installed, install it with homebrew by typing
brew install appledoc
.
- Install the documentation into Xcode by typing in the
appledoc .
root directory of the repository.
- Restart Xcode If it ' s already running.
1. If you have not installed Appledoc, first install the Appledoc
2. Type the Appledoc installation document on Xcode, under the root directory
3. Restart Xcode
Basic usage
Basic use
Consider you has a JSON like this:
Let's say your JSON string looks like this:
{"id": "ten""Country":"Germany""Dialcode""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:
- Create a class of your own and inherit to Jsonmodel
- Declare the JSON key value you need in your header file
#import "JSONModel.h"@interfaceCountrymodel:Jsonmodel@property(Assignnonatomic) int id @property (strongnonatomic< Span class= "P" >) nsstring* country; @property (strongnonatomicnsstring* dialcode; @property (assignnonatomicbool isineurope; @end
There ' s no need to do anything in the . M file.
You don't need to do anything else in the. m file.
- Initialize your model with data:
- Initialize your model as follows:
#import "CountryModel.h" ... nsstring* json = ( fetch here json from Internet nserror* err = nil countrymodel* country = [[countrymodel alloc initwithstring:< span class= "n" >json error:&err
If the validation of the JSON passes you has all the corresponding properties in your model populated from the JSON. Jsonmodel would also try to convert as much data to the types you expect, in the example above it would:
If the JSON passed in is legal, all of the properties you define will match that JSON value, and Jsonmodel will try to convert as much as possible into the data you want, as in the example above:
- Convert "id" from string (in the JSON) to a 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
- Conversion "id" from string to int type
- Copy the value of the country property
- Convert Dialcode, convert from NSNumber to NSString value
- The last one is the attribute that converts isineurope to BOOL
And the good news is all of had to does is define the properties and their expected types.
So all you have to do is define the attributes you want.
Online Tutorials
Online tutorials
Official website:http://www.jsonmodel.com
Class Docs online:http://jsonmodel.com/docs/
Step-by-Step tutorials:
Fool Tutorial:
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
Example
Automatic Name based mapping
Named Auto-match
{ "id": "123", "name": "Product name", "price": 12.95} |
@interface Productmodel:jsonmodel@property (assign, nonatomic) int id; @property (strong, nonatomic) nsstring* name; @pro Perty (assign, nonatomic) float price; @end @implementation Productmodel@end |
Model cascading (models including other models)
The model contains other model
{ "order_id": 104, "Total_price": 13.45, "product": { "id": "123", "name": "Product Name", " Price ": 12.95 }} |
productmodel* product; @end @implementation Ordermodel@end |
Model Collections
The model contains a collection of other 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 nsarray<productmodel>* Products; @end @implementation Ordermodel@end |
Key Mapping
Key value Reversal match
{ "order_id": 104, "Order_Details": [ { "name": "Product#1", "price": { "USD": 12.95 } } ]} |
@interface Ordermodel:jsonmodel@property (assign, nonatomic) int id; @property (assign, nonatomic) float price; @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)
Set global key-value reversal match
[Jsonmodel setglobalkeymapper:[ [Jsonkeymapper alloc] initwithdictionary:@{ @ "item_id": @ "id", @ "Item.name": @ "ItemName" }] ]; |
Map automatically under_score case to CamelCase
Convert the glide line to an initial capital letter
{ "order_id": 104, "order_product": @ "product#1", "Order_price": 12.95} |
@interface Ordermodel:jsonmodel@property (assign, nonatomic) int orderId; @property (assign, nonatomic) float Orderprice ; @property (Strong, nonatomic) nsstring* orderproduct; @end @implementation ordermodel+ (jsonkeymapper*) keyMapper{ [Jsonkeymapper mapperfromunderscorecasetocamelcase];} @end |
Optional properties (i.e. can be missing or null)
Can be an empty property value
{ "id": "123", "name": null, "price": 12.95} |
@interface Productmodel:jsonmodel@property (assign, nonatomic) int id; @property (strong, nonatomic) NSString< optional>* name; @property (assign, nonatomic) float price; @property (Strong, nonatomic) NSNumber< optional>* UUID; @end @implementation Productmodel@end |
Ignored properties (i.e. Jsonmodel completely ignores them)
Ignore some properties
{ "id": "123", "name": null} |
@interface Productmodel:jsonmodel@property (assign, nonatomic) int id; @property (strong, nonatomic) NSString< ignore>* customproperty; @end @implementation Productmodel@end |
Make all model properties optional (avoid if possible)
Allow all properties to have an empty property value
@implementation ProductModel+ (BOOL) propertyisoptional: (nsstring*) propertyname{ return YES; @end |
Lazy Convert collection items from dictionaries to models
Convert collection elements to 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 } ]} |
Convertondemand>* products, @end @implementation ordermodel@end |
Using the built-in thin HTTP client
Using built-in HTTP links
//AddExtraHeaders[[JsonhttpclientRequestheaders]SetValue:@"MySecret"Forkey:@"Authorizationtoken"];MAkePost,GetRequests[JsonhttpclientPostjsonfromurlwithstring:@ "Http://mydomain.com/api" params: @{@ "postParam1" : @ "value1" } completion:^ (id jsonjsonmodelerror *err) {//check errprocess json .}];
Export model to nsdictionary or to JSON text
Export model to a dictionary or JSON string
ProductModel*Pm=[[ProductModelAlloc]Initwithstring:JsonstringError:nil]; Pm. Name = @ "Changed Name" ; convert to dictionarynsdictionary * dict = [pm Todictionary]; convert to textnsstring* string = [pm tojsonstring ]
- JSON validation
- Data transformations
- Error handling
- Custom Data validation
- Automatic Compare and equality features
- and more.
- JSON data key-value matching
- Data conversion
- Good fault-tolerant ability
- Custom Data Key-value matching
- Automatic comparison and characterization of judgments
- There's more waiting for a kiss to dig
The following are the test results I used
Magical Data Modelling Framework for JSON