Json
发布时间:2022-04-22 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了JSONKit 使用示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
JSONKit是Object-C@L_675_1@处理json数据的库,非常高效而且易用,对比同类型的库有非常明显的性能优势,见下图:
JSON和Object-C中数据类型的映射关系如下表所示
下面写@L_675_1@简单的程序使用一下JSONKit(只需下载头文件以及源文件,放在项目目录下)
@H_
607_93@
@H_197_103@@H_874_106@#import<Foundation/Foundation.h>
- @H_874_106@#import"lib/JSONKit.h"
@H_197_103@
- int@H_601_105@main(intargc,constchar*argv[]){
@H_197_103@NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
@H_197_103@NSString*res=nil;
@H_197_103@
@H_197_103@
@H_197_103@
- NSString*str=@"thisisansString";
@H_197_103@res=[strJSONString];
- NSLog(@"res=%@",[NSStringStringWithString:res]);
@H_197_103@
@H_197_103@
@H_197_103@NSArray*arr=[[NSArrayalloc]initWithObjects:@"One",@"Two",@"Three",nil];
- res=[arrJSONString];
@H_197_103@NSLog(@"res=%@",[NSStringStringWithString:res]);
- [arrrelease];
@H_197_103@
@H_197_103@
@H_197_103@NSArray*arr1=[NSArrayarrayWithObjects:@"dog",@"cat",nil];
- NSArray*arr2=[NSArrayarrayWithObjects:[NSnumbernumberWithBool:YES],[NSnumbernumberWithInt:30],nil];
@H_197_103@NSDictionary*Dic=[NSDictionaryDictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];
- res=[DicJSONString];
@H_197_103@NSLog(@"res=%@",[NSStringStringWithString:res]);
@H_197_103@
@H_197_103@
@H_197_103@
- JSONDecoder*jd=[[JSONDecoderalloc]init];
@H_197_103@
@H_197_103@NSData*data=[DicJSONData];
- NSDictionary*ret=[jdobjectWithData:data];
@H_197_103@NSLog(@"res=%@",[retobjectForKey:@"pets"]);
@H_197_103@
@H_197_103@
- NSLog(@"res=%@",[[retobjectForKey:@"other"]objectATindex:0]);
@H_197_103@
@H_197_103@
- NSString*nstr=[DicJSONString];
@H_197_103@NSDictionary*ret2=[jdobjectWithUTF8String:(constunsignedchar*)[nstrUTF8String]length:(unsignedint)[nstrlength]];
- NSLog(@"res=%d",[[ret2objectForKey:@"pets"]indexOfObject:@"cat"]);
@H_197_103@
- NSLog(@"res=%@",[[ret2objectForKey:@"other"]objectATindex:1]);
@H_197_103@
@H_197_103@[jdrelease];
@H_197_103@[pooldrain];
- return0;
@H_197_103@}