The SBJson package is in the previous article.
You can use the key-value pairs in NSDictionary to splice Json data, which is very convenient and can also be nested and directly run the Code:
// Start splicing the Json string NSDictionary * dataDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "mac", @ "mac", @ "game", @ "game", @ "devicetoken ", @ "devicetoken", @ "device", @ "device", @ "gv", @ "gv", @ "lang", @ "lang", @ "OS ", @ "OS", nil]; NSDictionary * parmDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "getSession", @ "act", dataDictionary, @ "data", nil]; NSDictionary * jsonDictionary = [NSDictionary dictionary: @ "pv", @ "pv", parmDictionary, @ "param", nil]; SBJsonWriter * writer = [[SBJsonWriter alloc] init]; NSString * jasonString = [writer stringWithObject: jsonDictionary]; NSLog (@ "% @", jasonString );
The above Code uses layer-3 nesting. Note that in NSDictionary, values are preceded by keys.
The result after splicing is as follows:
{"pv":"pv","param":{"act":"getSession","data":{"os":"os","mac":"mac","game":"game","gv":"gv","lang":"lang","devicetoken":"devicetoken","device":"device"}}}
After parsing using the Json online verification tool:
{ "pv": "pv", "param": { "act": "getSession", "data": { "os": "os", "mac": "mac", "game": "game", "gv": "gv", "lang": "lang", "devicetoken": "devicetoken", "device": "device" } }}