JSON解析
NSURL *url = [NSURL URLWithString:URL_JSON];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//系統(tǒng)自帶的json解析
_Dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//分區(qū)個(gè)數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _Dic.count;
}
分區(qū)行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = [_Dic.allKeys objectAtIndex:section];
return [_Dic[key] count];
}
分區(qū)單元格內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
靜態(tài)字符串
static NSString *ID = @"cyy";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
NSString *Strkey = [_Dic.allKeys objectAtIndex:indexPath.section];
cell.textLabel.text = [[_Dic [Strkey]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.detailTextLabel.text = [[_Dic [Strkey]objectAtIndex:indexPath.row]objectForKey:@"age"];
return cell;
}
XML解析
- (void)viewDidLoad {? ? [super viewDidLoad];? ? ? ? NSURL *url = [[NSURL alloc]initWithString:SAXURL];? ? //初始化session 對象? ? NSURLSession *session = [NSURLSession sharedSession];? ? //請求URL連接? ? NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {? ? ? ? //sax 解析 初始化NSXMLParser對象? ? ? ? NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];? ? ? ? //設(shè)置代理? ? ? ? parser.delegate = self;? ? ? ? //開始解析? ? ? ? BOOL is = [parser parse];? ? ? ? ? ? ? ? if (is) {? ? ? ? ? ? NSLog(@"解析成功");? ? ? ? }else{? ? ? ? ? ? NSLog(@"解析失敗");? ? ? ? }? ? ? ? ? ? } ];? ? //開始請求? ? [task resume];? ? ? ? ? ? ? ? theTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];? ? ? ? theTable.delegate = self;? ? theTable.dataSource = self;? ? ? ? ? ? [self.view addSubview:theTable];? ? }//開始解析- (void)parserDidStartDocument:(NSXMLParser *)parser{? ? //初始化數(shù)組? ? _allData = [NSMutableArray array];}//遇到開始標(biāo)簽會自動回調(diào)- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary*)attributeDict{
if ([elementName isEqualToString:@"student"]) {
//初始化一個(gè)Student對象
stu = [[Student alloc]init];
//將數(shù)據(jù)添加到數(shù)組
[_allData addObject:stu];
}
//記錄開始標(biāo)簽的名字
_newElementName = elementName;
}
//遇到內(nèi)容會自動回調(diào) 兩個(gè)標(biāo)簽之間的內(nèi)容 空格和回車都會被當(dāng)做內(nèi)容調(diào)用
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([_newElementName isEqualToString:@"name"]) {
stu.name = string;
}else if ([_newElementName isEqualToString:@"age"]){
stu.age = string;
}else if ([_newElementName isEqualToString:@"gender"]){
stu.gender = string;
}
}
//遇到結(jié)束標(biāo)簽會自動回調(diào)
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{
_newElementName = nil;
[theTable reloadData];
}
//解析完畢
- (void)parserDidEndDocument:(NSXMLParser *)parser{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _allData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cyy";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.textLabel.text = [[_allData objectAtIndex:indexPath.row]name];
cell.detailTextLabel.text = [[_allData objectAtIndex:indexPath.row]age];
UILabel *oneLb = [[UILabel alloc]initWithFrame:CGRectMake(270, 0, 80, 40)];
oneLb.text = [[_allData objectAtIndex:indexPath.row]gender];
[cell.contentView addSubview:oneLb];
return cell;
}