1.简单的JSONKit 包下的 转换
首先我们需要引入JSONKit.h,m文件.百度一下就能下载了.
//假如 str就是网络获取的json文件
NSString *str = [NSString stringWithFormat:@"{\"id\":1,\"age\":\"2\"}"];
NSDictionary *resultsDictionary = [str objectFromJSONString];//转化为字典类型的文件
NSLog(@"%@",resultsDictionary);
// insert code here...
NSLog(@"Hello, World!");
//dic文件转化为json文件
NSMutableDictionary *people = [NSMutableDictionary dictionary];
NSMutableDictionary *boy = [NSMutableDictionary dictionary];
NSMutableDictionary *girl= [NSMutableDictionary dictionary];
NSMutableDictionary *cat = [NSMutableDictionary dictionary];
NSMutableDictionary *pandon = [NSMutableDictionary dictionary];
NSMutableDictionary *mammalia = [NSMutableDictionary dictionary];//哺乳类
NSMutableDictionary *repilia = [NSMutableDictionary dictionary];//爬行类
NSMutableDictionary *animal = [NSMutableDictionary dictionary];//
[boy setObject:@"李蝉" forKey:@"lc"];
[boy setObject:@"赵明伟" forKey:@"zz"];
[girl setObject:@"李若" forKey:@"lr"];
[girl setObject:@"徐娜" forKey:@"xn"];
[people setObject:boy forKey:@"boy"];
[people setObject:girl forKey:@"girl"];
[pandon setObject:@"大熊猫" forKey:@"大pandon"];
[pandon setObject:@"小猫" forKey:@"小pandon"];
[cat setObject:pandon forKey:@"cat"];
[mammalia setObject:people forKey:@"people"];
[mammalia setObject:pandon forKey:@"pandon"];
[animal setObject:mammalia forKey:@"animal"];
[repilia setObject:@"pa虫" forKey:@"repilia"];
[animal setObject:repilia forKey:@"爬行类"];
NSString *string = [animal JSONString];
NSLog(@"%@",string);
2013-11-29 11:34:51.692 JSON解析[1431:303] {
age = 2;
id = 1;
}
2013-11-29 11:34:51.694 JSON解析[1431:303] Hello, World!
2013-11-29 11:34:51.697 JSON解析[1431:303] {"爬行类":{"repilia":"pa虫"},"animal":{"pandon":{"大pandon":"大熊猫","小pandon":"小猫"},"people":{"boy":{"lc":"李蝉","zz":"赵明伟"},"girl":{"lr":"李若","xn":"徐娜"}}}}
Program ended with exit code: 0
/*
* json格式编码
*/
NSString *res = nil;
//字符串
NSString *str = @"this is a nsstring";
res = [str JSONString];
NSLog(@"res= %@", [NSString stringWithString: res]);
//res= "this is a nsstring"
//数组
NSArray *arr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil];
res = [arr JSONString];
NSLog(@"res= %@", [NSString stringWithString: res]);
[arr release];
//res= ["One","Two","Three"]
//字典类型(对象)
NSArray *arr1 = [NSArray arrayWithObjects:@"dog",@"cat",nil];
NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithInt:30],nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];
res = [dic JSONString];
NSLog(@"res= %@", [NSString stringWithString: res]);
//res= {"pets":["dog","cat"],"other":[true,30]}
/*
* json格式解码
*/
JSONDecoder *jd=[[JSONDecoder alloc] init];
//针对NSData数据
NSData *data = [dic JSONData];
NSDictionary *ret = [jd objectWithData: data];
NSLog(@"res= %@", [ret objectForKey:@"pets"]);
//res= (
// dog,
// cat
//)
NSLog(@"res= %@", [[ret objectForKey:@"other"] objectAtIndex:0]);
//res= 1
//针对NSString字符串数据
NSString *nstr = [dic JSONString];
NSDictionary *ret2 = [jd objectWithUTF8String:(const unsigned char *)[nstr UTF8String] length:(unsigned int)[nstr length]];
NSLog(@"res= %d", [[ret2 objectForKey:@"pets"] indexOfObject:@"cat"]);
//res= 1
NSLog(@"res= %@", [[ret2 objectForKey:@"other"] objectAtIndex:1]);
//res= 30