IOS笔记Json解析
新版SDK自身提供Json解析库,为了向下兼容很多程序仍会采用其他类库来解析,以下就是用JSONKit解析的例子
NSLog(@"打印测试");
NSString *jsonstring =@"[{\"age\":18,\"book\":{\"price\":23.2,\"title\":\"boook111\"},\"name\":\"samyou\"},{\"age\":22,\"book\":{\"price\":33,\"title\":\"booook222\"},\"name\":\"samsam\"}]";
NSData *data=[jsonstring dataUsingEncoding:NSUTF8StringEncoding];
NSArray *arr=(NSArray *)[data mutableObjectFromJSONData];
NSLog(@"count=%d",arr.count);
for(int i=0;i<arr.count;i++)
{
NSDictionary *people=[arr objectAtIndex:i];
NSString *name=[people objectForKey:@"name"];
NSString *age=[people objectForKey:@"age"];
NSLog(@"person withname=%@,age=%d",name,[age intValue]);
NSDictionary *book=[people objectForKey:@"book"];
NSString *bookname=[book objectForKey:@"title"];
NSNumber *price=[book objectForKey:@"price"];
NSLog(@"book with title=%@,price=%f",bookname,[price doubleValue]);
}
浙公网安备 33010602011771号