1.ios parsing JSON
Using the open source JSON package, project address:
http://stig.github.com/json-framework/
NSData * ResponseData = [Respones responsedata];
NSString * Strresponser = [[NSString alloc] Initwithdata:responsedata encoding:nsutf8stringencoding];
Sbjsonparser * parser = [[Sbjsonparser alloc]init];
Nsmutabledictionary *dicmessageinfo = [parser objectwithstring:strresponser]; Parse into JSON parse object
[Parser release];
Sent by
NSString * sender = [dicmessageinfo objectforkey:@ "Sender"];
2.json Nested Object parsing:
The string to upload
NSString *datastr=[[nsstring Alloc] initwithstring:@ "{\" cross\ ": {\" 1\ ": \" true\ ", \" 2\ ": \" false\ ", \" 3\ ": \" True\ "}}" ];
Get response return string
NSData * ResponseData = [Respones responsedata];
NSString * Strresponser = [[NSString alloc] Initwithdata:responsedata encoding:nsutf8stringencoding];
Nested parsing
Sbjsonparser * parser = [[Sbjsonparser alloc]init];
Nsmutabledictionary *dicmessageinfo = [parser objectwithstring:strresponser]; Parse into JSON parse object
Nsmutabledictionary * Cross = [dicmessageinfo objectforkey:@ ' Cross '];
NSString *cross1= [Cross objectforkey:@ "1"];
Parsing JSON to individual strings
Sent by
[Parser release];
NSLog (@ "Cross1:%@", CROSS1);
3. Stitching JSON strings
By using the method of the Sbjsonwriter class in Sbjson-(nsstring*) Stringwithobject: (ID) value can format the values in an object as JSON strings, conforming to key/ Data in value format can be formatted using this method after it is encapsulated in nsdictionary, and other data is formatted by stitching the string.
The method of class nsmutablestring can be used during splicing:
-(void) appendString: (NSString *) astring;
-(void) AppendFormat: (NSString *) format, ... (ns_format_function);
Dynamically add strings.
The stitched string can be validated in the form of JSON online validation, with the following URL:
http://jsonlint.com/
-(NSString *) getjsonstring
{
nsmutablestring *json = [nsmutablestring stringwithcapacity:128];
NSString *jsonstring=nil;
Sbjsonwriter *writer = [[Sbjsonwriter alloc] init];
[json appendstring:@ "{\" data\ ": {"];
[JSON appendformat:@ "\"%@\ ": \"%d\ "," @ "reset", reset];
if (missionstatus!=null)
{
Jsonstring=[writer Stringwithobject:status];
if (jsonstring!=null)
{
[JSON appendstring:@ "\" status\ ":"];
[JSON appendstring:jsonstring];
}
}
[JSON appendstring:@ "}}"];
return JSON;
}
4. Use multiple nsdictionary, stitching multi-layered nested JSON strings, reduce the JSON format error caused by hand stitching forgotten Quotes
Example code:
Nsdictionary *datadictionary= [nsdictionary dictionarywithobjectsandkeys:mac,@ "Mac",
game,@ "Game",
devicetoken,@ "Devicetoken",
device,@ "Device",
gv,@ "GV",
lang,@ "Lang",
os,@ "OS",
hardware,@ "Hardware",
down,@ "Down", nil];
Nsdictionary *parmdictionary= [nsdictionary dictionarywithobjectsandkeys:@ "getsession", @ "Act",
datadictionary,@ "Data", nil];
nsdictionary *jsondictionary=[nsdictionary dictionarywithobjectsandkeys:pv,@ "PV",  
parmdictionary,@ "param", nil];  
sbjsonwriter *writer = [[Sbjsonwriter alloc] init];  
 
NSString *jsonstring=nil;  
jsonstring=[writer stringwithobject: Jsondictionary];  
NSLog (@ "%@", jsonstring);  
5.json string online check URL:  
/http/ jsonlint.com/