Convert model object to JSON text or JSON binary file
Https://github.com/casatwy/AnyJson
Note: After testing, the dictionary or array cannot be processed directly
Main source of comments
AJTransformer.h and AJTRANSFORMER.M
////AJTransformer.h//Anyjson////Created by Casa on 14-9-19.//Copyright (c) 2014 Casa. All rights reserved.//#import<Foundation/Foundation.h>@protocolajserializable;@interfaceAjserializer:nsobject/** * convert an object into a JSON binary file * * @param Object Object * * @return JSON binary file*/+ (NSData *) Jsondatawithobject: (ID)Object;/** * Convert object to JSON String * * @param Object Object * * @return JSON string*/+ (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"@implementationAjserializer#pragmaMark-public method+ (NSData *) Jsondatawithobject: (ID)Object{ IDBasicobject = [Ajobject2jsonserializer serializetobasicobject:Object]; return[Nsjsonserialization datawithjsonobject:basicobject options:0Error:nil];}+ (NSString *) Jsonstringwithobject: (ID)Object{nsstring*jsonstring = [[NSString alloc] Initwithdata:[ajserializer jsondatawithobject:Object] encoding:nsutf8stringencoding]; returnjsonstring;}+ (ID) Objectwithjsondata: (NSData *) Jsondata Targetobjectclass: (Class) targetobjectclass{IDJsonobject = [nsjsonserialization jsonobjectwithdata:jsondata options:0Error: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 class:
////Playground.h//Anyjson////Created by Casa on 14-9-20.//Copyright (c) 2014 Casa. All rights reserved.//#import<Foundation/Foundation.h>#import "OtherObject.h"@interfacePlayground:nsobject@property (nonatomic, assign) Nsinteger Testinteger, @property (nonatomic, Strong) Otherobject /c5>*Otherobject;@end
////OtherObject.h//Anyjson////Created by Casa on 14-9-21.//Copyright (c) 2014 Casa. All rights reserved.//#import<Foundation/Foundation.h>@interfaceOtherobject:nsobject@property (nonatomic, assign) Nsinteger Testinteger; @property (nonatomic, assign) BOOL I Stest, @property (nonatomic, strong) NSString*Name: @property (nonatomic, assign)CharTestchar;@end
Use the source code:
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"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Playground*playground =[[Playground alloc] init]; Otherobject*otherobject =[[Otherobject alloc] init]; Otherobject.testinteger= -; Otherobject.istest=YES; Otherobject.name=@"Casa"; Otherobject.testchar='C'; Playground.otherobject=Otherobject; NSString*jsonstring =[Ajserializer Jsonstringwithobject:playground]; NSLog (@"JSON string is%@", jsonstring);}@end
Printed information:
2015-01-04 20:52:18.399 anyjson[3825:149779] JSON string is {"Testinteger": 0, "Otherobject": {"istest": True, "name": " Casa "," Testinteger ":" Testchar ":" C "}}
A few points to note:
-Here is the attribute contained in the property-
Convert model object to JSON text or JSON binary file