zoukankan      html  css  js  c++  java
  • 解析稍微复杂一点的数据

    碰到如上图所示的数据需要我们解析,用"name"的值作为分区头标题,用上图所圈起来的字典作为一个model,我们可以用两种方式解析:

    第一种方式:就是使用一个数组和一个字典,数组用来存储"name"里的值,作为key;字典用来存储所有数据;

    第二种方式:就是使用两个数组,其中一个数组用来存储"name"的值,另一个数组存储所有数据;

    闲言修叙:直接上代码

    第一种方式:

      1 #import "RootTableViewController.h"
      2 #import "YHCell.h"
      3 
      4 @interface RootTableViewController ()
      5 
      6 @property (nonatomic, strong) NSMutableArray *allKeysArray;
      7 @property (nonatomic, strong) NSMutableDictionary *allDataDict;
      8 
      9 @end
     10 
     11 @implementation RootTableViewController
     12 
     13 // 懒加载
     14 - (NSMutableDictionary *)allDataDict {
     15     
     16     if (!_allDataDict) {
     17         _allDataDict = [NSMutableDictionary dictionary];
     18     }
     19     return _allDataDict;
     20 }
     21 
     22 - (NSMutableArray *)allKeysArray {
     23     
     24     if (!_allKeysArray) {
     25         _allKeysArray = [NSMutableArray array];
     26     }
     27     return _allKeysArray;
     28 }
     29 
     30 
     31 - (void)viewDidLoad {
     32     [super viewDidLoad];
     33     
     34     self.navigationItem.title = @"旅游";
     35     self.tableView.showsVerticalScrollIndicator = NO;
     36     
     37     
     38     // 数据处理
     39     [self loadWebData];
     40     
     41     
     42     // 注册cell
     43     [self.tableView registerClass:[YHCell class] forCellReuseIdentifier:@"cell"];
     44     
     45 }
     46 
     47 
     48 // 数据处理
     49 - (void)loadWebData {
     50     NSURL *url = [NSURL URLWithString:@"http://api.yhouse.com/m/city/dynmiclist"];
     51     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     52     NSURLSession *session = [NSURLSession sharedSession];
     53     
     54     NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
     55         if (error == nil) {
     56             
     57             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
     58             NSDictionary *dict1 = dict[@"data"];
     59             NSArray *array = dict1[@"allCity"];
     60             
     61             for (int i = 0; i < array.count; i++) {
     62                 NSDictionary *dict2 = array[i];
     63                 
     64                 NSMutableArray *dataArray = [NSMutableArray array];
     65                 for (NSString *key in dict2) {
     66                     
     67                     if ([key isEqualToString:@"name"]) {
     68                         NSString *value = dict2[key];
     69                         [self.allKeysArray addObject:value];
     70                     }
     71                     
     72                     if ([key isEqualToString:@"tabs"]) {
     73                         for (NSDictionary *dict3 in dict2[key]) {
     74                             Model *model = [[Model alloc] init];
     75                             [model setValuesForKeysWithDictionary:dict3];                        
     76                             [dataArray addObject:model];
     77                         }
     78                     }
     79                 }
     80                 [self.allDataDict setObject:dataArray forKey:[self.allKeysArray lastObject]];
     81             }
     82             [self.tableView reloadData];
     83         }
     84     }];
     85     
     86     [task resume];
     87 }
     88 
     89 // 设置分区个数
     90 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     91     
     92     return self.dataHandle.allKeysArray.count;
     93 }
     94 
     95 // 设置每个分区的行数
     96 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     97     
     98     NSString *key = self.dataHandle.allKeysArray[section];
     99     NSInteger index = [self.dataHandle.allDataDict[key] count];
    100     return index;
    101 }
    102 
    103 
    104 // 返回cell
    105 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    106     
    107     YHCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    108 
    109     Model *model = [[Model alloc] init];
    110     NSString *key = self.dataHandle.allKeysArray[indexPath.section];
    111     model = self.dataHandle.allDataDict[key][indexPath.row];
    112     
    113     // 给cell赋值
    114     [cell bindModel:model];
    115 
    116     
    117     return cell;
    118 }
    119 
    120 
    121 // 设置cell高度
    122 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    123     
    124     return 50;
    125 }
    126 
    127 
    128 // 设置头标题
    129 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    130     
    131     return self.dataHandle.allKeysArray[section];
    132 }
    133 
    134 @end

    第一种方式中间的解析数据也可以全用foin循环来解析,也就是还可以简化一点代码:

     1 - (void)loadWebData {
     2     NSURL *url = [NSURL URLWithString:@"http://api.yhouse.com/m/city/dynmiclist"];
     3     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     4     NSURLSession *session = [NSURLSession sharedSession];
     5     
     6     NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
     7         if (error == nil) {
     8             
     9             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    10             NSDictionary *dict1 = dict[@"data"];
    11             NSArray *array = dict1[@"allCity"];
    12             
    13             for (NSDictionary *dict2 in array) {
    14                 [self.allKeysArray addObject:dict2[@"name"]];
    15                 
    16                 NSMutableArray *array1 = [NSMutableArray array];
    17                 for (NSDictionary *dict3 in dict2[@"tabs"]) {
    18                     Model *model = [[Model alloc] init];
    19                     [model setValuesForKeysWithDictionary:dict3];
    20                     [array1 addObject:model];
    21                 }
    22                 [self.allDataDict setObject:array1 forKey:[self.allKeysArray lastObject]];
    23             }
    24         }
    25         [self.tableView reloadData];
    26     }];
    27     
    28     [task resume];
    29 }

    第二种方法:

      1 #import "RootTableViewController.h"
      2 #import "YHCell.h"
      3 
      4 @interface RootTableViewController ()
      5 
      6 @property (nonatomic, strong) NSMutableArray *allKeysArray;
      7 @property (nonatomic ,strong) NSMutableArray *allDataArray;
      8 
      9 @end
     10 
     11 @implementation RootTableViewController
     12 
     13 // 懒加载
     14 - (NSMutableArray *)allKeysArray {
     15     
     16     if (!_allKeysArray) {
     17         _allKeysArray = [NSMutableArray array];
     18     }
     19     return _allKeysArray;
     20 }
     21 
     22 - (NSMutableArray *)allDataArray {
     23     
     24     if (!_allDataArray) {
     25         _allDataArray = [NSMutableArray array];
     26     }
     27     return _allDataArray;
     28 }
     29 
     30 
     31 
     32 - (void)viewDidLoad {
     33     [super viewDidLoad];
     34     
     35     
     36     self.navigationItem.title = @"旅游";
     37     self.tableView.showsVerticalScrollIndicator = NO;
     38     
     39     
     40     // 数据处理
     41     [self loadData];
     42     
     43     
     44     // 注册cell
     45     [self.tableView registerClass:[YHCell class] forCellReuseIdentifier:@"cell"];
     46     
     47 }
     48 
     49 
     50 // 数据处理
     51 - (void)loadData {
     52     
     53     NSURL *url = [NSURL URLWithString:@"http://api.yhouse.com/m/city/dynmiclist"];
     54     
     55     NSURLSession *session = [NSURLSession sharedSession];
     56     NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
     57         
     58         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
     59         NSDictionary *dict1 = dict[@"data"];
     60         NSArray *array = dict1[@"allCity"];
     61         
     62         for (NSDictionary *dict2 in array) {
     63             
     64             NSMutableArray *array1 =[NSMutableArray array];
     65     
     66             [self.allKeysArray addObject:dict2[@"name"]];
     67             
     68             for (NSDictionary *dict3 in dict2[@"tabs"]) {
     69                 Model *model = [[Model alloc] init];
     70                 [model setValuesForKeysWithDictionary:dict3];
     71                 [array1 addObject:model];
     72             }
     73             
     74             [self.allDataArray addObject:array1];
     75         }
     76         [self.tableView reloadData];
     77         
     78     }];
     79     
     80     [task resume];
     81 
     82 }
     83 
     84 
     85 // 分区个数
     86 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     87     
     88     return self.allKeysArray.count;
     89 }
     90 
     91 // 每个分区的行数
     92 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     93     
     94     return [self.allDataArray[section] count];
     95 }
     96 
     97 
     98 // 返回cell
     99 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    100     
    101     YHCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    102     
    103     Model *model = [[Model alloc] init];
    104     NSArray *array = self.allDataArray[indexPath.section];
    105     model = array[indexPath.row];
    106     
    107     // 给cell赋值
    108     [cell bindModel:model];
    109     
    110     
    111     return cell;
    112 }
    113 
    114 
    115 // 设置cell高度
    116 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    117     
    118     return 50;
    119 }
    120 
    121 
    122 // 设置头标题
    123 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    124     
    125     return self.allKeysArray[section];
    126 }
    127 
    128 @end
  • 相关阅读:
    Documentum常见问题1—Tomcat应用内存溢出
    Documentum常见问题3—保存搜索Saved Searches提示用户对文件柜无权限
    Request.QueryString[]和Request[]的区别
    将money转换成大写汉字
    Windows API(一) 什么是Windows API
    C#将数据导出到Excel汇总
    开始—运行—命令
    手动绘制DataTable
    触发器Deleted表和Inserted表
    功能强大的Regsvr32命令
  • 原文地址:https://www.cnblogs.com/zhizunbao/p/5493294.html
Copyright © 2011-2022 走看看