国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ios中使用SBJson拼接和解析json

2019-11-14 20:15:45
字體:
來源:轉載
供稿:網友

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]; 
     //發(fā)送者 
     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到各個字符串 
            //發(fā)送者 
            [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); 
動態(tài)添加字符串。 
拼接的字符串可通過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/


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 正安县| 同心县| 新和县| 双流县| 信阳市| 浮梁县| 雷州市| 汤阴县| 浏阳市| 道真| 迭部县| 双柏县| 清徐县| 闽侯县| 清镇市| 延庆县| 建湖县| 枣庄市| 饶阳县| 阿拉善右旗| 闸北区| 大港区| 娄底市| 梁平县| 封丘县| 弥渡县| 西乌珠穆沁旗| 永春县| 柳州市| 兰考县| 新营市| 余江县| 黔西| 吉林省| 固安县| 宁远县| 怀集县| 祁东县| 宜兰市| 承德市| 邛崃市|