Convert model object to JSON text or JSON binary file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.