ios 使用json

jopen 11年前發布 | 28K 次閱讀 IOS iOS開發 移動開發

1、從https://github.com/stig/json-framework/中下載json框架:json-framework

2、解壓下載的包,將class文件夾下的所有文件導入到當前工程下。

3、在使用的文件中加入導入語句 :#import "SBJson.h"

4、將json字符串轉為NSDictionary對象

  1. //測試json的解析  
  2. -(void)testJsonParser: (NSString *) jsonString  
  3. {  
  4.     jsonString = [[NSString alloc] initWithString:@"{\"userInfo\":{\"userName\":\"張三\",\"sex\":\"男\"}}"];  
  5.     NSLog(@"正在解析json字符串是:%@",jsonString);  
  6.       
  7.     SBJsonParser * parser = [[SBJsonParser alloc] init];  
  8.     NSError * error = nil;  
  9.     NSMutableDictionary *jsonDic = [parser objectWithString:jsonString error:&error];  
  10.     NSMutableDictionary * dicUserInfo = [jsonDic objectForKey:@"userInfo"];  
  11.       
  12.     NSLog(@"%@",[jsonDic objectForKey:@"userInfo" ]);  
  13.     NSLog(@"%@",[dicUserInfo objectForKey:@"userName"]);  
  14.     NSLog(@"%@",[dicUserInfo objectForKey:@"sex"]);  
  15. }  
  16. </ol> </div>
    5、 處理json對象有多個記錄的方法</span>

    1.         NSString * customerGridJsonString = [[NSString alloc]initWithString:@" {\"customer\":[{\"name\":\"roamer\",\"ycount\":\"232.4\",\"sumcount\": \"322.3\"},{\"name\":\"王三\",\"ycount\":\"221.2\",\"sumcount\":\"1123.2 \"},{\"name\":\"李四\",\"ycount\":\"1221.2\",\"sumcount\":\"12123.2\"}]}"];  
    2.           
    3.         SBJsonParser * parser = [[SBJsonParser alloc] init];  
    4. //        NSLog(@"%@",customerGridJsonString);  
    5.         NSError * error = nil;  
    6.           
    7.         NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithDictionary:[parser objectWithString:customerGridJsonString error:&error]];  
    8.         NSLog(@"%@",root);  
    9.         //注意轉換代碼  
    10.         SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];  
    11.           
    12.         NSString *jsonString = [jsonWriter stringWithObject:root];  
    13.           
    14.         [jsonWriter release];  
    15.         NSLog(@"%@",jsonString);  
    16.         //注意轉換代碼  
    17.         NSMutableArray * customers = [root objectForKey:@"customer"];  
    18.         NSLog(@"%@",customers);  
    19.         for(NSMutableDictionary * member  in customers)  
    20.         {  
    21.             NSLog(@"%@",[[member objectForKey:@"name"] description]);  
    22.         }  
    23. </ol> </div>

      6、遞歸遍歷解析出的NSDictionary對象

      1. -(void)visitDict:(NSDictionary *)dict{    
      2.   NSArray *keys=[dict allKeys];    
      3.   for (NSString *key in keys) {    
      4.      NSString *result=[NSString stringWithFormat:@"key=%@,value=%@",key,[dict objectForKey:key]];    
      5.      NSLog(result);    
      6.      if([[dict objectForKey:key] isKindOfClass:[NSDictionary class]]){    
      7.             [self visitDict:[dict objectForKey:key]];    
      8.      }    
      9.    }    
      10. }    
      11. </ol> </div>
        7、將解析出的NSDictionary對象還原為json字符串 
        </span>


        </div> NSString * jsonStr=[items JSONRepresentation]; </span>

         本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
         轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
         本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!