Splicing and parsing json and sbjsonjson in ios

Source: Internet
Author: User
Tags jsonlint

Splicing and parsing json and sbjsonjson in ios

1. parse json in ios
Use 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 it into a json parsing object
[Parser release];
// Sender
NSString * sender = [dicMessageInfo objectForKey: @ "sender"];

2. json nested object parsing:
// String to be uploaded
NSString * dataStr = [[NSString alloc] initWithString: @ "{\" cross \ ": {\" 1 \ ": \" true \ ", \" 2 \": \ "false \", \ "3 \": \ "true \"} "];
// Obtain the response 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 it into a json parsing object

NSMutableDictionary * cross = [dicMessageInfo objectForKey: @ "cross"];

NSString * cross1 = [cross objectForKey: @ "1"];
// Parse json to each string
// Sender
[Parser release];
NSLog (@ "cross1: % @", cross1 );
3. concatenate A json string

You can use the SBJsonWriter class method in SBJson-(NSString *) stringWithObject :( id) value to format the value in an object as a json string, you can use this method to format data in key/value format after it is encapsulated into NSDictionary. Other data is formatted by concatenating strings.
You can use NSMutableString-like methods during splicing:
-(Void) appendString :( NSString *) aString ;,
-(Void) appendFormat :( NSString *) format,... NS_FORMAT_FUNCTION (1, 2 );
Dynamically add strings.
The format of the spliced string can be verified by json online verification. The URL is:
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. concatenate multiple NSDictionary json strings with multiple layers of nesting to reduce json format errors caused by manual splicing and missing quotation marks
Sample 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 verification 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.