OC中如何把字典中的数据拼接成URL字符串
1个回答
展开全部
可以使用NSDictionary中的键值对来拼接Json数据,非常方便,也可以进行嵌套,直接上代码:
//开始拼接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"
}
}
}
//开始拼接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"
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询