//開始拼接Json字符串
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 dictionaryWithObjectsAndKeys:@"pv",@"pv",
parmDictionary,@"param",nil];
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSString *jasonString = [writer stringWithObject:jsonDictionary];
NSLog(@"%@",jasonString);
上面的代碼用到了三層嵌套,註意的是,NSDictionary中,前面的是值,後面的是鍵。
拼接後的結果如下:
{"pv":"pv","param":{"act":"getSession","data":{"os":"os","mac":"mac","game":"game","gv":"gv","lang":"lang","devicetoken":"devicetoken","device":"device"}}}
使用 Json在線校驗工具 解析後:
{
"pv": "pv",
"param": {
"act": "getSession",
"data": {
"os": "os",
"mac": "mac",
"game": "game",
"gv": "gv",
"lang": "lang",
"devicetoken": "devicetoken",
"device": "device"
}
}
}