JSON解析(引入第三方JSONKit)
Posted 恒远也
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON解析(引入第三方JSONKit)相关的知识,希望对你有一定的参考价值。
1 #import "MovieTableViewController.h" 2 #import "JSONKit.h" 3 #import "Movie.h" 4 #import "MovieTableViewCell.h" 5 @interface MovieTableViewController () 6 @property (nonatomic,strong) NSMutableArray *movieArray; 7 @property (nonatomic,strong) NSMutableArray *varietyArray; 8 @end 9 10 @implementation MovieTableViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 //创建JSON解码器 15 //返回JSONData的可变容器. 16 NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"movie" ofType:@"txt"]]; 17 NSMutableArray *jsonArray = [[JSONDecoder decoder] mutableObjectWithData:jsonData]; 18 //NSLog(@"%@",jsonArray); 19 20 NSDictionary *movieDic = [jsonArray objectAtIndex:0]; 21 NSDictionary *varietyDic = jsonArray[1]; 22 23 //解析热门电影 24 NSArray *movieArray = [movieDic objectForKey:@"data"]; 25 //初始化电影数组 26 self.movieArray = [NSMutableArray new]; 27 //循环遍历字典 28 for (NSDictionary *movieItem in movieArray) { 29 Movie *movie = [Movie new]; 30 [movie setValuesForKeysWithDictionary:movieItem]; 31 [self.movieArray addObject:movie]; 32 33 } 34 for (Movie *movie in self.movieArray) { 35 NSLog(@"%@",movie.title); 36 } 37 38 //解析综艺 39 NSDictionary *varietyArray = varietyDic[@"data"]; 40 //初始化综艺数组 41 self.varietyArray = [NSMutableArray new]; 42 for (NSDictionary *varietyItem in varietyArray) { 43 Movie *variety = [Movie new]; 44 [variety setValuesForKeysWithDictionary:varietyItem]; 45 [self.varietyArray addObject:variety]; 46 } 47 for (Movie *movie in self.varietyArray) { 48 NSLog(@"%@",movie.title); 49 } 50 51 }
以上是关于JSON解析(引入第三方JSONKit)的主要内容,如果未能解决你的问题,请参考以下文章