iOS 使用SBJSON创建和解析JSON

原文地址:http://blog.csdn.net/gf771115/article/details/7718403

//创建JSON
NSDictionary *dictonary = [[NSMutableDictionary alloc] init];
[dictonary setValue:@"MIle" forKey:@"name"];
[dictonary setValue:@"19" forKey:@"age"];
[dictonary setValue:@"male" forKey:@"sex"];
NSDictionary *root = [[NSMutableDictionary alloc] init];
[root setValue:dictonary forKey:@"player"];

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSLog(@"Start Create JSON!");
NSString *value = [writer stringWithObject:root];
NSLog(value);

//解析JSON
NSDictionary *dicRoot = [[[SBJsonParser alloc]init] objectWithString:value];
for (int i=0; i<[dicRoot count]; i++) {
NSDictionary *player = [dicRoot objectForKey:@"player"];
NSString *name = [player objectForKey:@"name"];
NSString *age = [player objectForKey:@"age"];
NSString *sex = [player objectForKey:@"sex"];
NSLog(@"Name = %@", name);
NSLog(@"Age = %@", age);
NSLog(@"Sex = %@", sex);

}

你可能感兴趣的:(json)