ios 中使用SBJson拼接和解析json,sbjsonjson

來源:互聯網
上載者:User

ios 中使用SBJson拼接和解析json,sbjsonjson

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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.