方便且简单的 JSON 数据建模框架 - 非常适用于网络协议,支持继承和嵌套。
将
NSObject+JsonImpl.h
和NSObject+JsonImpl.m
复制到您的 projext。
或
JsonImpl 可以通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
pod "JsonImpl"
要运行示例项目,请克隆仓库,并首先从 Example 目录运行 pod install
@interface Human : NSObject
@property (nonatomic, assign) NSInteger age;
@end
...
NSDictionary *dict = @{@"age":@30};
Human *man = [Human new];
[man parse:dict];
NSString *jsonStr = @"{\n \"age\" : 30\n}";
Man *man2 = [Man new];
[man2 parse: jsonStr];
Human *man = [Human new];
NSString *str = [man toJsonString];
NSDictionary *dict = [man toJsonDictionary];
@interface Man : Human
@property (nonatomic, strong) NSString *job;
@end
@interface Man : Human
@property (nonatomic, strong) Human* wife;
@end
@interface Man : Human
@property (nonatomic, strong) NSArray* childrens;
@end
@implementation Man
+ (void)initialize
{
if (self == [Man class]) {
[self setArrayProperty:@"childrens" withClass:@"Human"];
}
}
如果属性名以 '_ignore' 结尾,则该属性将被忽略
如果您在 initialize
中调用了 setIgnoreProperty
,则属性将被忽略
如果属性包含 <Ignore>
,它也将被忽略
@interface Man : Human
@property (nonatomic, strong) NSString<Ignore>* stringProperty;
@property (nonatomic, strong) NSString* stringProperty_ignore;
@property (nonatomic, assign) NSInteger intProperty;
@end
@implementation Man
+ (void)initialize
{
if (self == [Man class]) {
[self setIgnoreProperty:@"intProperty"];
}
}
@end
wanglin.sun, [email protected]
JsonImpl 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。