• ios 中使用SBJson拼接和解析json


    1.ios解析json
    使用开源json包,项目地址:
         http://stig.github.com/json-framework/
    NSData * responseData = [respones responseData];
          
         NSString * strResponser = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    SBJsonParser * parser = [[SBJsonParser alloc]init];
         NSMutableDictionary *dicMessageInfo = [parser objectWithString:strResponser]; // 解析成json解析对象
    [parser release];
         //发送者
         NSString * sender = [dicMessageInfo objectForKey:@"sender"];

    2.json嵌套对象解析:
    //要上传的字符串
        NSString *dataStr=[[NSString alloc] initWithString:@"{"cross":{"1":"true","2":"false","3":"true"}}"];
    //获取响应返回字符串
    NSData * responseData = [respones responseData];
            
            NSString * strResponser = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    //嵌套解析
    SBJsonParser * parser = [[SBJsonParser alloc]init];
                
                NSMutableDictionary *dicMessageInfo = [parser objectWithString:strResponser]; // 解析成json解析对象
                
                NSMutableDictionary * cross = [dicMessageInfo objectForKey:@"cross"];
                
                NSString *cross1= [cross objectForKey:@"1"];
                //解析json到各个字符串
                //发送者
                [parser release];
                NSLog(@"cross1: %@",cross1);
    3.拼接json字符串

    通过使用SBJson中的SBJsonWriter类的方法- (NSString*)stringWithObject:(id)value可以将一个对象中的值格式化为json字符串,符合key/value格式的数据封装到NSDictionary后可以使用该方法进行格式化,其他数据通过拼接字符串的方式格式化。
    在拼接过程中可以使用类NSMutableString的方法:
    - (void)appendString:(NSString *)aString;、
    - (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
    动态添加字符串。
    拼接的字符串可通过json在线验证的方式验证其格式是否正确,网址为:
    http://jsonlint.com/
    -(NSString *) getJsonString
    {
        NSMutableString *json = [NSMutableString stringWithCapacity:128];
        NSString *jsonString=nil;
        SBJsonWriter *writer = [[SBJsonWriter alloc] init];
        [json appendString:@"{"data":{"];
        [json appendFormat:@""%@":"%d",",@"reset",reset];
        if(missionStatus!=NULL)
        {
            jsonString=[writer stringWithObject:status];
            if(jsonString!=NULL)
            {
                [json appendString:@""status":"];
                [json appendString:jsonString];
            }
        }
        [json appendString:@"}}"];
        return json;
    }
    4.利用多个NSDictionary,拼接多层嵌套的json字符串,减少因手工拼接忘记加引号导致的json格式错误
    示例代码:
    NSDictionary *dataDictionary= [NSDictionary dictionaryWithObjectsAndKeys:mac,@"mac",
                                       game,@"game",
                                       devicetoken,@"devicetoken",
                                       device,@"device",
                                       gv,@"gv",
                                       lang,@"lang",
                                       os,@"os",
                                       hardware,@"hardware",
                                       down,@"down",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 *jsonString=nil;
        jsonString=[writer stringWithObject:jsonDictionary];
        NSLog(@"%@",jsonString);
    5.json字符串在线校验网址:
    http://jsonlint.com/

  • 相关阅读:
    非常高兴,“万能数据库查询分析器”中英文3.01版本测试完成,会尽快完成发布,敬请等待
    OpenGL学习入门之VS2010环境配置
    服务器异常处理:Java Logger
    从3.01版本开始,“万能数据库查询分析器”中英文版本将全部免费
    java算法:顺序查找(有监视哨和无监视哨)
    DELPHI第三方控件及组件大全(安装方法与使用)
    Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析
    非常高兴,《彻底删除文件》2.01版本也测试完成,会尽快完成发布,敬请等待
    无Dll插入进程,下载者VC源代码
    剖析WININIT.INI文件与Windows病毒
  • 原文地址:https://www.cnblogs.com/lovewx/p/4283035.html
Copyright © 2020-2023  润新知