使用 JSONModel
Magical Data Modelling Framework for JSON

https://github.com/icanzilb/JSONModel
New: In version 0.12.0 I added experimental support for exporting JSON models to CoreData.
最新消息:在0.12.0版本中,我试验性的支持将 JSON models 转化成 CoreData .
Give it a try and let me know, post an issue or just get in touch. Try something like that:
如果你试验过了,有空告知哥一下,哥写开源库也不容易,发一篇博文或者给个链接以表支持:
NSError* error = nil;
GitHubRepoEntity* entity = [GitHubRepoEntity entityWithModel:model
inContext:self.managedObjectContext
error:&error];
[self.managedObjectContext save: 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)长期关注这个开源项目,2)你是土豪的话,给哥捐点吧,谢谢.
JSONModel 是一个库,他能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它.
Adding JSONModel to your project
添加 JSONModel 到你的工程中
Requirements
需要的环境:
- 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 a zip file or 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's Podfile add the JSONModel pod:
使用 Cocoa pods 来安装:
pod 'JSONModel'
If you want to read more about CocoaPods, have a look at this short tutorial.
如果你不会用 CocoaPods,你可以看看这简单的教程。
Source code documentation
源码的文档
The source code includes class docs, which you can build yourself and import into Xcode:
源码本身包含了类的文档,你可以自己编译后导入到你的Xcode中:
- If you don't already have appledoc installed, install it with homebrew by typing
brew install appledoc. - Install the documentation into Xcode by typing
appledoc .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:
假设你的 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:
- 创建一个你自己的类,并继承至 JSONModel
- 在你的头文件里面进行声明你所需要的 JSON key值
#import "JSONModel.h" @interface CountryModel : JSONModel @property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* country;
@property (strong, nonatomic) NSString* dialCode;
@property (assign, nonatomic) BOOL isInEurope; @end
There's no need to do anything in the .m file.
.m文件中你不需要做其他的事情了.
- Initialize your model with data:
- 初始化你的 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 合法,你所定义的所有的属性都会与该 JSON 值相匹配,并且 JSONModel 也会尝试尽可能的转换成你所想要的数据,就像上面的例子:
- 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
- 转化 "id",从字符串转换成 int 型
- 拷贝 country 属性的值
- 转换 dialCode ,从NSNumber 转换为 NSString 值
- 最后一个呢是将 isInEurope 转换成 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:
傻瓜教程:
Examples
例子
Automatic name based mapping
命名自动匹配
{
|
@interface ProductModel : JSONModel |
Model cascading (models including other models)
model中含有其他的model
{
|
@interface OrderModel : JSONModel |
Model collections
model中含有其他model的集合
{
|
@protocol ProductModel |
Key mapping
键值转回匹配
{
|
@interface OrderModel : JSONModel |
Global key mapping (applies to all models in your app)
设置全局的键值转回匹配
[JSONModel setGlobalKeyMapper:[ |
Map automatically under_score case to camelCase
将下滑线转换成首字母大写
{
|
@interface OrderModel : JSONModel @property (assign, nonatomic) int orderId; |
Optional properties (i.e. can be missing or null)
可以为空的属性值
{
|
@interface ProductModel : JSONModel |
Ignored properties (i.e. JSONModel completely ignores them)
忽略某些属性
{
|
@interface ProductModel : JSONModel |
Make all model properties optional (avoid if possible)
让所有的属性都可以有空的属性值
@implementation ProductModel |
Lazy convert collection items from dictionaries to models
将集合元素转换成 model
{
|
@protocol ProductModel |
Using the built-in thin HTTP client
使用内置的 HTTP 链接
//add extra headers
[[JSONHTTPClient requestHeaders] setValue:@"MySecret" forKey:@"AuthorizationToken"]; //make post, get requests
[JSONHTTPClient postJSONFromURLWithString:@"http://mydomain.com/api"
params:@{@"postParam1":@"value1"}
completion:^(id json, JSONModelError *err) { //check err, process json ... }];
Export model to NSDictionary or to JSON text
将 model 导出为字典或者json字符串
ProductModel* pm = [[ProductModel alloc] initWithString:jsonString error:nil];
pm.name = @"Changed Name"; //convert to dictionary
NSDictionary* dict = [pm toDictionary]; //convert to text
NSString* string = [pm toJSONString];
- json validation
- data transformations
- error handling
- custom data validation
- automatic compare and equality features
- and more.
- json数据键值匹配
- 数据转换
- 好的容错能力
- 自定义数据键值匹配
- 自动比较以及判断的特性
- 还有更多的等待亲来挖掘
以下是本人使用的测试结果



使用 JSONModel的更多相关文章
- 【iOS】在Swift中使用JSONModel
前言 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验. 声明 欢 ...
- iOS JsonModel 的使用
本文转自:http://blog.csdn.net/smking/article/details/40432287 下面讲一下JSONModel的使用方法. @inteface MyModel : J ...
- JSONModel对架构的影响及解决方案
越来越多的项目使用CocoaPods,使用CocoaPods很有可能会用过JSONModel. JSONModel是个很强大的库,只要根据JSON定义好对应的类并继承JSONModel,就可以把JSO ...
- JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray
JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray
- iOS中JSONModel的使用
iOS中JSONModel的使用 流弊的JSON数据模型框架 https://github.com/jsonmodel/jsonmodel 版本 1.3.0 如果你喜欢JSONModel,并且使用 ...
- JSONModel - 字符串换转实体类
JSONModel https://github.com/icanzilb/JSONModel/ 一. 获取属性的元数据 const char *attrs = property_getAttrib ...
- JSONModel 遇见关键字为id或者description
像id.description这样的,都是系统自带的,要解析它,得特殊处理一下.我用的是JSONModel { "contentList": [ { "id": ...
- JSONModel的基本使用
JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它. 使用前准备 添加 JSONModel 到你的工程中 1.需要的环境: A ...
- CocoaPods 报错 [!] Error installing JSONModel
pod install p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #34bd26 } span.s1 { } ...
- CocoaPods 报错 [!] The dependency `JSONModel (~> 1.2.0)` is not used in any concrete target.
当用CocoaPods pod install 时出现了下面的错误时: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; col ...
随机推荐
- Numpy基础总结
一.文件读取 numpy.genfromtxt() 可以用来读取各种文件.常用语法大致如下: numpy.genfromtxt(fname, dtype=<type 'float'>, d ...
- Druid连接池(四)
十七.Druid加密 运维和DBA都不希望把密码明文直接写在配置文件中,Druid提供了数据库密码加密的功能. ConfigFilter的作用包括: 从配置文件中读取配置 从远程http文件中读取配置 ...
- java ee 中 Jsp 页面的定时的跳转(数字倒数)
java ee 中 Jsp 页面的定时的跳转,实现数字倒计时跳转固定页面 1,Servlet类 RefreshServlet类实现 package org.servlet; import java. ...
- ruby 中的 module
Module是Class的父类: >> Class.superclass => Module module 没有实例变量 module 没有new不能生成实例对象 module内可以 ...
- Swift基础语法之变量函数
import Foundation //变量声明使用 //使用 let 来声明常量,使用 var 来声明变量 常量只能为它赋值一次 let name="cuiyw"; var ag ...
- pictureBox控件获得图片路径的三种方法及自适应大小属性
1.绝对路径: this.pictureBox2.Image=Image.FromFile("D:\\001.jpg"); 2.相对路径: Application.StartupP ...
- c#基础学习(0806)之StringBuilder的使用
以前字符串的拼接基本都是用string来完成的,从来没有考虑过性能或者速度的问题,自从学习了StringBuilder之后才发现两者的差距有多大,当然,数据量比较小的时候,用string还是挺方便的, ...
- 写给创业者:如何打造让用户离不开的App
百度权重查询 词库网 网站监控 服务器监控 SEO监控 Swift编程语言教程 还记得那个学会编程,开发了一个拼车应用程序——Trees For Cars,并因此发了一笔小财的流浪汉吗?今天的新闻称, ...
- javaScript 简单的时间格式转换【转】
转自:http://blog.csdn.net/lxl_family/article/details/38693903.根据时间戳,转成相对的字符串形式 function timeStamp2Stri ...
- idea入手配置
一.编码配置 utf-8 二.注释模板(Java Doc) 三.Live Templates 快捷键模板, 如配置logger,(类似 sout)
