• 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 }
  • 相关阅读:
    最深叶节点的最近公共祖先
    ML-Agents(十)Crawler
    ML-Agents(九)Wall Jump
    ML-Agents(八)PushBlock
    ML-Agents(七)训练指令与训练配置文件
    Unity Editor扩展编辑器中显示脚本属性
    ML-Agents(六)Tennis
    数据结构(二)—栈
    ML-Agents(五)GridWorld
    ML-Agents(四)3DBall补充の引入泛化
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5285021.html
Copyright © 2020-2023  润新知