Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了JSONKit 使用示例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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


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

numberArrayObject
JSON Objective-C
null NSNull
trueandfalse NSNumber
String NSString
NSArray
NSDictionary

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

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

大佬总结

以上是大佬教程为你收集整理的JSONKit 使用示例全部内容,希望文章能够帮你解决JSONKit 使用示例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。