Use Sbjson stitching and parsing json in iOS

Source: Internet
Author: User
Tags jsonlint

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/

Related Article

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.