JSONKit 使用示例

JSONKit是Object-C一个处理json数据的库,非常高效而且易用,对比同类型的库有非常明显的性能优势,见下图:


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

JSONObjective-C
nullNSNull
true and falseNSNumber
NumberNSNumber
StringNSString
ArrayNSArray
ObjectNSDictionary

下面写一个简单的程序使用一下JSONKit(只需下载头文件以及源文件,放在项目目录下

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "lib/JSONKit.h"  
  3.   
  4. int main (int argc, const char * argv[]) {  
  5.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  6.   
  7.     NSString *res = nil;  
  8.       
  9.     /* 
  10.      * json格式编码 
  11.      */  
  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.     [pool drain];  
  64.     return 0;  
  65. }  

JSONKit的接口中还可以自行定制序列化和反序列化选项,针对如何提升效率作者也是给了很多使用的建议,例如尽量使用NSData来替换NSString类型,因为JSON数据通常是用来通信使用,而通信过程使用NSData类型更为高效,毕竟是二进制流数据更短,所以没必要转成NSString多此一举了。不知道我理解得对不对。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值