解析稍微复杂一点的数据
生活随笔
收集整理的這篇文章主要介紹了
解析稍微复杂一点的数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
碰到如上圖所示的數(shù)據(jù)需要我們解析,用"name"的值作為分區(qū)頭標(biāo)題,用上圖所圈起來(lái)的字典作為一個(gè)model,我們可以用兩種方式解析:
第一種方式:就是使用一個(gè)數(shù)組和一個(gè)字典,數(shù)組用來(lái)存儲(chǔ)"name"里的值,作為key;字典用來(lái)存儲(chǔ)所有數(shù)據(jù);
第二種方式:就是使用兩個(gè)數(shù)組,其中一個(gè)數(shù)組用來(lái)存儲(chǔ)"name"的值,另一個(gè)數(shù)組存儲(chǔ)所有數(shù)據(jù);
閑言修敘:直接上代碼
第一種方式:
#import "RootTableViewController.h" #import "YHCell.h"@interface RootTableViewController ()@property (nonatomic, strong) NSMutableArray *allKeysArray; @property (nonatomic, strong) NSMutableDictionary *allDataDict;@end@implementation RootTableViewController// 懶加載 - (NSMutableDictionary *)allDataDict {if (!_allDataDict) {_allDataDict = [NSMutableDictionary dictionary];}return _allDataDict; }- (NSMutableArray *)allKeysArray {if (!_allKeysArray) {_allKeysArray = [NSMutableArray array];}return _allKeysArray; }- (void)viewDidLoad {[super viewDidLoad];self.navigationItem.title = @"旅游";self.tableView.showsVerticalScrollIndicator = NO;// 數(shù)據(jù)處理 [self loadWebData];// 注冊(cè)cell[self.tableView registerClass:[YHCell class] forCellReuseIdentifier:@"cell"];}// 數(shù)據(jù)處理 - (void)loadWebData {NSURL *url = [NSURL URLWithString:@"http://api.yhouse.com/m/city/dynmiclist"];NSURLRequest *request = [NSURLRequest requestWithURL:url];NSURLSession *session = [NSURLSession sharedSession];NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {if (error == nil) {NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];NSDictionary *dict1 = dict[@"data"];NSArray *array = dict1[@"allCity"];for (int i = 0; i < array.count; i++) {NSDictionary *dict2 = array[i];NSMutableArray *dataArray = [NSMutableArray array];for (NSString *key in dict2) {if ([key isEqualToString:@"name"]) {NSString *value = dict2[key];[self.allKeysArray addObject:value];}if ([key isEqualToString:@"tabs"]) {for (NSDictionary *dict3 in dict2[key]) {Model *model = [[Model alloc] init];[model setValuesForKeysWithDictionary:dict3]; [dataArray addObject:model];}}}[self.allDataDict setObject:dataArray forKey:[self.allKeysArray lastObject]];}[self.tableView reloadData];}}];[task resume]; }// 設(shè)置分區(qū)個(gè)數(shù) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return self.dataHandle.allKeysArray.count; }// 設(shè)置每個(gè)分區(qū)的行數(shù) - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {NSString *key = self.dataHandle.allKeysArray[section];NSInteger index = [self.dataHandle.allDataDict[key] count];return index; }// 返回cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {YHCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];Model *model = [[Model alloc] init];NSString *key = self.dataHandle.allKeysArray[indexPath.section];model = self.dataHandle.allDataDict[key][indexPath.row];// 給cell賦值 [cell bindModel:model];return cell; }// 設(shè)置cell高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 50; }// 設(shè)置頭標(biāo)題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {return self.dataHandle.allKeysArray[section]; }@end第一種方式中間的解析數(shù)據(jù)也可以全用foin循環(huán)來(lái)解析,也就是還可以簡(jiǎn)化一點(diǎn)代碼:
- (void)loadWebData {NSURL *url = [NSURL URLWithString:@"http://api.yhouse.com/m/city/dynmiclist"];NSURLRequest *request = [NSURLRequest requestWithURL:url];NSURLSession *session = [NSURLSession sharedSession];NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {if (error == nil) {NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];NSDictionary *dict1 = dict[@"data"];NSArray *array = dict1[@"allCity"];for (NSDictionary *dict2 in array) {[self.allKeysArray addObject:dict2[@"name"]];NSMutableArray *array1 = [NSMutableArray array];for (NSDictionary *dict3 in dict2[@"tabs"]) {Model *model = [[Model alloc] init];[model setValuesForKeysWithDictionary:dict3];[array1 addObject:model];}[self.allDataDict setObject:array1 forKey:[self.allKeysArray lastObject]];}}[self.tableView reloadData];}];[task resume]; }第二種方法:
#import "RootTableViewController.h" #import "YHCell.h"@interface RootTableViewController ()@property (nonatomic, strong) NSMutableArray *allKeysArray; @property (nonatomic ,strong) NSMutableArray *allDataArray;@end@implementation RootTableViewController// 懶加載 - (NSMutableArray *)allKeysArray {if (!_allKeysArray) {_allKeysArray = [NSMutableArray array];}return _allKeysArray; }- (NSMutableArray *)allDataArray {if (!_allDataArray) {_allDataArray = [NSMutableArray array];}return _allDataArray; }- (void)viewDidLoad {[super viewDidLoad];self.navigationItem.title = @"旅游";self.tableView.showsVerticalScrollIndicator = NO;// 數(shù)據(jù)處理 [self loadData];// 注冊(cè)cell[self.tableView registerClass:[YHCell class] forCellReuseIdentifier:@"cell"];}// 數(shù)據(jù)處理 - (void)loadData {NSURL *url = [NSURL URLWithString:@"http://api.yhouse.com/m/city/dynmiclist"];NSURLSession *session = [NSURLSession sharedSession];NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];NSDictionary *dict1 = dict[@"data"];NSArray *array = dict1[@"allCity"];for (NSDictionary *dict2 in array) {NSMutableArray *array1 =[NSMutableArray array];[self.allKeysArray addObject:dict2[@"name"]];for (NSDictionary *dict3 in dict2[@"tabs"]) {Model *model = [[Model alloc] init];[model setValuesForKeysWithDictionary:dict3];[array1 addObject:model];}[self.allDataArray addObject:array1];}[self.tableView reloadData];}];[task resume];}// 分區(qū)個(gè)數(shù) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return self.allKeysArray.count; }// 每個(gè)分區(qū)的行數(shù) - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [self.allDataArray[section] count]; }// 返回cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {YHCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];Model *model = [[Model alloc] init];NSArray *array = self.allDataArray[indexPath.section];model = array[indexPath.row];// 給cell賦值 [cell bindModel:model];return cell; }// 設(shè)置cell高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 50; }// 設(shè)置頭標(biāo)題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {return self.allKeysArray[section]; }@end?
轉(zhuǎn)載于:https://www.cnblogs.com/dongbaoyue/p/5525257.html
總結(jié)
以上是生活随笔為你收集整理的解析稍微复杂一点的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 启动app android,androi
- 下一篇: java实现快速排序以及快速排序的原理