JSONKit 使用示例

前端之家收集整理的这篇文章主要介绍了JSONKit 使用示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

@H_502_1@

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

@H_502_1@

@H_502_1@Number @H_502_1@Array @H_502_1@Object
@H_502_1@JSON @H_502_1@Objective-C
@H_502_1@null @H_502_1@NSNull
trueandfalse @H_502_1@NSNumber
@H_502_1@String @H_502_1@NSString
@H_502_1@NSArray
@H_502_1@NSDictionary

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

@H_502_1@[cpp] view plaincopy
  1. @H_502_1@#import<Foundation/Foundation.h>
  2. @H_502_1@#import"lib/JSONKit.h"
  3. @H_502_1@
  4. @H_502_1@intmain(intargc,constchar*argv[]){
  5. @H_502_1@NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
  6. @H_502_1@
  7. @H_502_1@NSString*res=nil;
  8. @H_502_1@
  9. @H_502_1@/*
  10. @H_502_1@*json格式编码
  11. @H_502_1@*/
  12. @H_502_1@
  13. @H_502_1@//字符串
  14. @H_502_1@NSString*str=@"thisisansstring";
  15. @H_502_1@res=[strJSONString];
  16. @H_502_1@NSLog(@"res=%@",[NSStringstringWithString:res]);
  17. @H_502_1@//res="thisisansstring"
  18. @H_502_1@
  19. @H_502_1@
  20. @H_502_1@//数组
  21. @H_502_1@NSArray*arr=[[NSArrayalloc]initWithObjects:@"One",@"Two",@"Three",nil];
  22. @H_502_1@res=[arrJSONString];
  23. @H_502_1@NSLog(@"res=%@",[NSStringstringWithString:res]);
  24. @H_502_1@[arrrelease];
  25. @H_502_1@//res=["One","Two","Three"]
  26. @H_502_1@
  27. @H_502_1@
  28. @H_502_1@//字典类型(对象)
  29. @H_502_1@NSArray*arr1=[NSArrayarrayWithObjects:@"dog",@"cat",nil];
  30. @H_502_1@NSArray*arr2=[NSArrayarrayWithObjects:[NSNumbernumberWithBool:YES],[NSNumbernumberWithInt:30],nil];
  31. @H_502_1@NSDictionary*dic=[NSDictionarydictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];
  32. @H_502_1@res=[dicJSONString];
  33. @H_502_1@NSLog(@"res=%@",[NSStringstringWithString:res]);
  34. @H_502_1@//res={"pets":["dog","cat"],"other":[true,30]}
  35. @H_502_1@
  36. @H_502_1@
  37. @H_502_1@/*
  38. @H_502_1@*json格式解码
  39. @H_502_1@*/
  40. @H_502_1@JSONDecoder*jd=[[JSONDecoderalloc]init];
  41. @H_502_1@
  42. @H_502_1@//针对NSData数据
  43. @H_502_1@NSData*data=[dicJSONData];
  44. @H_502_1@NSDictionary*ret=[jdobjectWithData:data];
  45. @H_502_1@NSLog(@"res=%@",[retobjectForKey:@"pets"]);
  46. @H_502_1@//res=(
  47. @H_502_1@//dog,
  48. @H_502_1@//cat
  49. @H_502_1@//)
  50. @H_502_1@NSLog(@"res=%@",[[retobjectForKey:@"other"]objectAtIndex:0]);
  51. @H_502_1@//res=1
  52. @H_502_1@
  53. @H_502_1@//针对NSString字符串数据
  54. @H_502_1@NSString*nstr=[dicJSONString];
  55. @H_502_1@NSDictionary*ret2=[jdobjectWithUTF8String:(constunsignedchar*)[nstrUTF8String]length:(unsignedint)[nstrlength]];
  56. @H_502_1@NSLog(@"res=%d",[[ret2objectForKey:@"pets"]indexOfObject:@"cat"]);
  57. @H_502_1@//res=1
  58. @H_502_1@NSLog(@"res=%@",[[ret2objectForKey:@"other"]objectAtIndex:1]);
  59. @H_502_1@//res=30
  60. @H_502_1@
  61. @H_502_1@[jdrelease];
  62. @H_502_1@
  63. @H_502_1@[pooldrain];
  64. @H_502_1@return0;
  65. @H_502_1@}
@H_502_1@JSONKit的接口中还可以自行定制序列化和反序列化选项

猜你在找的Json相关文章