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/