JSON解析的幾種方法?
JSONKit(第三方的解析工具) , NSJSONSerialization (系統(tǒng)提供的解析類,其解析效率最高)
普通方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"JSON_stu" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:path];
/*NSJSONReadingMutableContainers = (1UL << 0), //返回一個(gè)數(shù)組或者字典
NSJSONReadingMutableLeaves = (1UL << 1), //返回一個(gè)字符串
NSJSONReadingAllowFragments = (1UL << 2) //返回一個(gè)任意類型的對象
*/
//找到最小的數(shù)組套字典的
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"array == %@", array);
for (NSDictionary *dic in array) {
NSLog(@"dic === %@", [dic objectForKey:@"content"]);
}
}
調(diào)用第三方庫的方法
- 文件夾中添加資源文件
- 引入第三方框架 #import "JSONKit.h"
- 修改混編模式
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"JSON_stu" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSArray *array = [data objectFromJSONData];
NSLog(@"array = %@",array);
}