• 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 }
  • 相关阅读:
    python使用 db.select 返回的数据只能遍历一次
    redis学习主从配置
    php操作EXCLE(通过phpExcle实现读excel数据)
    php操作EXCLE(通过phpExcle实现向excel写数据)
    Redis 源码安装
    MongoDb环境安装
    php下载
    漂亮的jQuery tab选项卡插件
    jquery禁用右键、文本选择功能、复制按键的实现
    jquery中邮箱地址 URL网站地址正则验证实例
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5285021.html
Copyright © 2020-2023  润新知