JSON/NSDictionary 对象映射框架
要运行示例项目,请克隆仓库,并先从 Example 目录运行 pod install
。
首先,将您的模型更新为扩展 JFObject 类并实现 +load 方法,例如。
#import "JFObject.h"
@class JFTestSubObject;
@interface JFTestObject : JFObject
@property(nonatomic, retain) NSString *testString;
@property(nonatomic, retain) NSNumber *testNumber;
@property(nonatomic, retain) NSDictionary *testDictionary;
@property(nonatomic, retain) NSArray *testArray;
@property(nonatomic, retain) NSSet *testSet;
@property(nonatomic, retain) JFTestSubObject *testSubObject;
@end
@implementation JFTestObject
+ (void)load
{
[super load];
[[self metaData]
mapPropertyName:@"test_id" to:kInstanceIdPropertyName];
}
- (void)afterPropertiesSet
{
[super afterPropertiesSet];
NSLog(@"Object populated: %@", self);
}
@end
@interface JFTestSubObject : JFObject
@property(nonatomic, retain) NSString *title;
@end
@implementation JFTestSubObject
+ (void)load
{
[super load];
}
@end
然后您可以使用这些酷炫的功能
@protocol JFSerializableProtocol <NSObject>
- (NSDictionary *)toDictionary;
+ (NSArray *)toDictionaryArray:(NSArray *)array;
- (NSData *)toJson;
+ (NSData *)toJsonArray:(NSArray *)array;
- (NSString *)toJsonString;
+ (NSString *)toJsonArrayString:(NSArray *)array;
+ (instancetype)fromDictionary:(NSDictionary *)dictionary;
+ (NSArray *)fromDictionaryArray:(NSArray *)dictionaryArray;
+ (instancetype)fromJson:(NSData *)data;
+ (NSArray *)fromJsonArray:(NSData *)data;
+ (instancetype)fromJsonString:(NSString *)string;
+ (NSArray *)fromJsonArrayString:(NSString *)string;
- (void)afterPropertiesSet;
@end
请参见示例(单元测试)以获取更多详细信息。
Denis Jajčević,[email protected]
JFObjectMapping 可在 MIT 许可证下获得。有关更多信息,请参阅 LICENSE 文件。