/*
JSON文件使用键值对Key和Value的形式出现的
在IOS开发中使用JSON数据较多,一般使用的都是JSON解析数据比如一下一份JSON文件:
{
"students": [
{
"name":"张三",
"telephoneNumber":"1234567891",
"gender":"男"
},
{
"name":"李四",
"telephoneNumber":"1734995333",
"gender":"男"
}
]
}
解析步骤很简单单:
1.获取沙盒中JSON文件<说明:这里演示的是沙盒下的JSON解析,开发中通常使用的网络数据>
2.调用JSON解析器直接解析路径下的JSON文件存入字典中<Java中的Map集合>
*/
/*-----------------使用JSON解析------------------*/
//1.获取JSON文件
NSString *jsonString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"students" ofType:@"json"]encoding:NSUTF8StringEncoding error:nil];
//2.实例化JSON解析器
NSDictionary *jsonSerial = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
NSArray *value = [jsonSerial objectForKey:@"students"];
self.stuArray = [NSMutableArray array];
for (int i=0; i<value.count; i++) {
NSDictionary *dic = [value objectAtIndex:i];
Students *stus = [[Students alloc] init];
stus.name = [dic objectForKey:@"name"];
stus.telephoneNumber = [dic objectForKey:@"telephoneNumber"];
stus.gender = [dic objectForKey:@"gender"];
[self.stuArray addObject:stus];
[stus release],stus = nil;
}