码迷,mamicode.com
首页 > 移动开发 > 详细

IOS开发之——使用SBJson拼接Json字符串

时间:2014-05-07 03:22:16      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:ios开发   json   json拼接   

SBJson包的下载地址在上一篇文章中。

可以使用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"
        }
    }
}


IOS开发之——使用SBJson拼接Json字符串,布布扣,bubuko.com

IOS开发之——使用SBJson拼接Json字符串

标签:ios开发   json   json拼接   

原文地址:http://blog.csdn.net/zhenyu5211314/article/details/25030661

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!