stirng 读取json文件 jsonkit 使用

来源:互联网 发布:淘宝代理如何发货 编辑:程序博客网 时间:2024/06/07 17:42
nsstringjsonkit
[java] view plaincopy
  1. <pre name="code" class="java"><pre name="code" class="java">1.将字典或者数组反序列化为NSString.  
  2.     NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];  
  3.     NSMutableDictionary *alert = [NSMutableDictionary dictionary];  
  4.     NSMutableDictionary *aps = [NSMutableDictionary dictionary];  
  5.     [alert setObject:@"a msg come!" forKey:@"body"];  
  6.     [aps setObject:alert forKey:@"alert"];  
  7.     [aps setObject:@"3" forKey:@"bage" ];  
  8.     [aps setObject:@"def.mp3" forKey:@"sound"];  
  9.     [jsonDic setObject:aps forKey:@"aps"];  
  10.       
  11.     NSString *strJson = [jsonDic JSONString];  
  12.       
  13.     NSLog(@"%@",strJson);  
  14.       
  15.       
  16. //    2.将NSString反序列化为数组、字典.  
  17.     NSDictionary *result = [strJson  objectFromJSONString];  
  18. //    或者  
  19.     NSDictionary *result = [dataJson  objectFromJSONData];  
  20.       
  21.     NSLog(@"%@",result);</pre><br>  
  22. NSError *error;NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];if (textFileContents == nil) {NSLog(@"Error reading text file. %@", [error  
  23.  localizedFailureReason]);}NSDictionary *result = [textFileContents objectFromJSONString];NSLog(@"res= %@", [result objectForKey:@"cmd"]);  
  24. <pre></pre>  
  25. <br>  
  26. <br>  
  27. <pre></pre>  
  28. <p>读取jsonKIT文件,转化成json字典</p>  
  29. <p><br>  
  30. </p>  
  31.   
  32. </pre>  
原创粉丝点击