//開始拼接Json字符串
ns dictionary * data dictionary =[ns dictionary dictionaryWithObjectsAndKeys:@ " MAC " ,@ " MAC ",
@“遊戲”,@“遊戲”,
@ "設備令牌" ,@ "設備令牌",
@“設備”,@“設備”,
@“gv”,@“gv”,
@“郎”,@“郎”,
@"os",@"os ",nil];
ns dictionary * parm dictionary =[ns dictionary dictionaryWithObjectsAndKeys:@ " getSession " ,@ " act ",
dataDictionary,@"data ",nil];
ns dictionary * jsonDictionary =[ns dictionary dictionaryWithObjectsAndKeys:@ " PV " ,@ " PV ",
parmDictionary,@"param ",nil];
SBJsonWriter * writer =[[SBJsonWriter alloc]init];
ns string * jasonString =[writer string with object:jsonDictionary];
NSLog(@“% @”,Jason string);
上面的代碼使用了三層嵌套。註意,在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 ",
"數據":{
“OS”:“OS”,
“MAC”:“MAC”,
“遊戲”:“遊戲”,
《靈臺》:《靈臺》,
《郎》:“郎”,
"設備令牌":"設備令牌",
“設備”:“設備”
}
}
}