當前位置:成語大全網 - 漢語詞典 - 如何將字典中的數據拼接成OC中的URL字符串

如何將字典中的數據拼接成OC中的URL字符串

您可以使用NSDictionary中的鍵值對來拼接Json數據,這非常方便,或者您可以嵌套它們並直接對它們進行編碼:

//開始拼接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”,

“遊戲”:“遊戲”,

《靈臺》:《靈臺》,

《郎》:“郎”,

"設備令牌":"設備令牌",

“設備”:“設備”

}

}

}