将Model对象转换成json文本或者json二进制文件
将Model对象转换成json文本或者json二进制文件
https://github.com/casatwy/AnyJson
注意:经过测试,不能够直接处理字典或者数组
主要源码的注释
AJTransformer.h 与 AJTransformer.m
// // AJTransformer.h // AnyJson // // Created by casa on 14-9-19. // Copyright (c) 2014年 casa. All rights reserved. // #import <Foundation/Foundation.h> @protocol AJSerializable; @interface AJSerializer : NSObject /** * 将对象转换成json二进制文件 * * @param object 对象 * * @return json二进制文件 */ + (NSData *)jsonDataWithObject:(id)object; /** * 将对象转换成json字符串 * * @param object 对象 * * @return json字符串 */ + (NSString *)jsonStringWithObject:(id)object; + (id)objectWithJsonData:(NSData *)jsonData targetObjectClass:(Class)targetObjectClass; + (id)objectWithJsonString:(NSString *)jsonString targetObjectClass:(Class)targetObjectClass; @end
// // AJTransformer.m // AnyJson // // Created by casa on 14-9-19. // Copyright (c) 2014年 casa. All rights reserved. // #import "AJSerializer.h" #import "AJObject2JsonSerializer.h" #import "AJJson2ObjectSerializer.h" @implementation AJSerializer #pragma mark - public method + (NSData *)jsonDataWithObject:(id)object { id basicObject = [AJObject2JsonSerializer serializeToBasicObject:object]; return [NSJSONSerialization dataWithJSONObject:basicObject options:0 error:nil]; } + (NSString *)jsonStringWithObject:(id)object { NSString *jsonString = [[NSString alloc] initWithData:[AJSerializer jsonDataWithObject:object] encoding:NSUTF8StringEncoding]; return jsonString; } + (id)objectWithJsonData:(NSData *)jsonData targetObjectClass:(Class)targetObjectClass { id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; return [AJJson2ObjectSerializer transformJsonObject:jsonObject toTargetObjectClass:targetObjectClass]; } + (id)objectWithJsonString:(NSString *)jsonString targetObjectClass:(Class)targetObjectClass { NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; return [AJSerializer objectWithJsonData:jsonData targetObjectClass:targetObjectClass]; } @end
Model类:
// // Playground.h // AnyJson // // Created by casa on 14-9-20. // Copyright (c) 2014年 casa. All rights reserved. // #import <Foundation/Foundation.h> #import "OtherObject.h" @interface Playground : NSObject @property (nonatomic, assign) NSInteger testInteger; @property (nonatomic, strong) OtherObject *otherObject; @end
// // OtherObject.h // AnyJson // // Created by casa on 14-9-21. // Copyright (c) 2014年 casa. All rights reserved. // #import <Foundation/Foundation.h> @interface OtherObject : NSObject @property (nonatomic, assign) NSInteger testInteger; @property (nonatomic, assign) BOOL isTest; @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) char testChar; @end
使用的源码:
ViewController.m
// // ViewController.m // AnyJson // // Created by casa on 14-9-19. // Copyright (c) 2014年 casa. All rights reserved. // #import "ViewController.h" #import "AnyJson.h" #import "Playground.h" #import "AJClassHelper.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Playground *playground = [[Playground alloc] init]; OtherObject *otherObject = [[OtherObject alloc] init]; otherObject.testInteger = 13; otherObject.isTest = YES; otherObject.name = @"casa"; otherObject.testChar = ‘c‘; playground.otherObject = otherObject; NSString *jsonString = [AJSerializer jsonStringWithObject:playground]; NSLog(@"json string is %@", jsonString); } @end
打印的信息:
2015-01-04 20:52:18.399 AnyJson[3825:149779] json string is {"testInteger":0,"otherObject":{"isTest":true,"name":"casa","testInteger":13,"testChar":"c"}}
几个需要注意的地方:
-此处是属性中包含了属性-
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。