JSONModel
https://github.com/icanzilb/JSONModel/
一. 获取属性的元数据
const char *attrs = property_getAttributes(property);
propertyAttributes=T@"NSArray<OrderDetail><Optional>",&,N,V_OrderDetails
1. T
T表示普通属性
R表示只读属性
Tc,表示BOOL
2. @"NSArray<OrderDetail><Optional>"
@里面"<"之前是类型
@里面"<"和">"之间是协议
3. V_OrderDetails
V表示变量
后面的OrderDetails表示变量名称
二.获取对象的父类
obj.superclass
[isKindOfClass:class]
原文地址:http://www.cnblogs.com/YouXianMing/p/3638648.html使用 JSONModelPosted on 2014-04-01 20:30 游贤明 阅读(205) 评论(0) 编辑 收藏Magical Data Modelling Framework for JSONhttps://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需要的环境:
Get it as: 1) source files
1. 下载 JSONModel zip包 2. 将 JSONModel 文件夹拷贝到你的工程项目中 3. 将库 SystemConfiguration.framework 添加上 or 2) via Cocoa podsIn 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中:
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}
#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文件中你不需要做其他的事情了.
#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 也会尝试尽可能的转换成你所想要的数据,就像上面的例子:
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命名自动匹配
Model cascading (models including other models)model中含有其他的model
Model collectionsmodel中含有其他model的集合
Key mapping键值转回匹配
Global key mapping (applies to all models in your app)设置全局的键值转回匹配
Map automatically under_score case to camelCase将下滑线转换成首字母大写
Optional properties (i.e. can be missing or null)可以为空的属性值
Ignored properties (i.e. JSONModel completely ignores them)忽略某些属性
Make all model properties optional (avoid if possible)让所有的属性都可以有空的属性值
Lazy convert collection items from dictionaries to models将集合元素转换成 model
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];
以下是本人使用的测试结果 |