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];
//系统自带的json解析
_Dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//分区个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _Dic.count;
}
分区行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = [_Dic.allKeys objectAtIndex:section];
return [_Dic[key] count];
}
分区单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
静态字符串
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]; //设置代理 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{ //初始化数组 _allData = [NSMutableArray array];}//遇到开始标签会自动回调- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary*)attributeDict{
if ([elementName isEqualToString:@"student"]) {
//初始化一个Student对象
stu = [[Student alloc]init];
//将数据添加到数组
[_allData addObject:stu];
}
//记录开始标签的名字
_newElementName = elementName;
}
//遇到内容会自动回调 两个标签之间的内容 空格和回车都会被当做内容调用
- (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;
}
}
//遇到结束标签会自动回调
- (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;
}