关于JSONKit的使用

   刚接触JSON解析的时候,使用的是SBJSON解析工具,知道了不同工具解析速度有偏差的时候才开始好好研究这个问题。下面有一个关于解析速度的柱状图,我们可以一目了然。

时间轴越短,解析越快;json-framework即SBJSON;

可以看到jsonkit 的解析速度还是很快的而SBJSON的速度让人失望,ios5 之后提供了NSJSONSerialization,其速度比jsonkit要快,但是只有ios5 之后才支持因此我选择使用jsonkit。

JSON和Object-C中数据类型的映射关系如下表所示

JSON Objective-C
null NSNull
true and false NSNumber
Number NSNumber
String NSString
Array NSArray
Object NSDictionary


首先要下载jsonkit文件,只有两个文件,这也更加证明使用它是很简单的。


下面写一个简单的程序使用一下JSONKit

  1. #import <Foundation/Foundation.h>  
  2. #import "JSONKit.h"  
  3.   
  4. int main (int argc, const char * argv[]) {  
  5.    
  6. @autorelease{
  7.   
  8.     NSString *res = nil;  
  9.       
  10.     /* 
  11.      * json格式编码 
  12.      */  
  13.     //字符串  
  14.     NSString *str = @"this is a nsstring";  
  15.     res = [str JSONString];  
  16.     NSLog(@"res= %@", [NSString stringWithString: res]);  
  17.     //res= "this is a nsstring"  
  18.       
  19.   
  20.     //数组  
  21.     NSArray *arr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil];  
  22.     res = [arr JSONString];  
  23.     NSLog(@"res= %@", [NSString stringWithString: res]);  
  24.     [arr release];  
  25.     //res= ["One","Two","Three"]  
  26.       
  27.   
  28.     //字典类型(对象)  
  29.     NSArray *arr1 = [NSArray arrayWithObjects:@"dog",@"cat",nil];  
  30.     NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithInt:30],nil];  
  31.     NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];  
  32.     res = [dic JSONString];  
  33.     NSLog(@"res= %@", [NSString stringWithString: res]);  
  34.     //res= {"pets":["dog","cat"],"other":[true,30]}   
  35.       
  36.       
  37.     /* 
  38.      * json格式解码 
  39.      */  
  40.     JSONDecoder *jd=[[JSONDecoder alloc] init];  
  41.       
  42.     //针对NSData数据  
  43.     NSData *data = [dic JSONData];  
  44.     NSDictionary *ret = [jd objectWithData: data];  
  45.     NSLog(@"res= %@", [ret objectForKey:@"pets"]);  
  46.     //res= (  
  47.     //  dog,  
  48.     //  cat  
  49.     //)  
  50.     NSLog(@"res= %@", [[ret objectForKey:@"other"] objectAtIndex:0]);  
  51.     //res= 1  
  52.       
  53.     //针对NSString字符串数据  
  54.     NSString *nstr = [dic JSONString];  
  55.     NSDictionary *ret2 = [jd objectWithUTF8String:(const unsigned char *)[nstr UTF8String] length:(unsigned int)[nstr length]];  
  56.     NSLog(@"res= %d", [[ret2 objectForKey:@"pets"] indexOfObject:@"cat"]);  
  57.     //res= 1  
  58.     NSLog(@"res= %@", [[ret2 objectForKey:@"other"] objectAtIndex:1]);  
  59.     //res= 30  
  60.       
  61.     [jd release];  
  62.       
  63.     }
  64.     return 0;  

常用的只有几个方法,很简单吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值