复习知识点:XML解析数据,JOSN解析数据,GET请求数据,POST请求数据
GDataXMLDocument-XML解析
1 #pragma mark - ======== Dom XML解析 2 - (IBAction)domParserActionXML_Document:(id)sender { 3 4 // 第一步 引入动态库 5 // 1 获取文件路径 6 NSString *Path = [[NSBundle mainBundle] pathForResource:@"StudentIfor_xml" ofType:@"txt"]; 7 // 2.根据路经获取data类型数据 8 NSData *data = [NSData dataWithContentsOfFile:Path]; 9 // 2.1 初始化存储数据的数组 10 self.dataArray = [NSMutableArray array]; 11 // 3.设置dom解析(创建解析文档) 12 GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; 13 // 4.获取根节点 14 GDataXMLElement *rootElement = document.rootElement; 15 // 5.遍历获取相对应的子节点 16 for (GDataXMLElement *studentElement in rootElement.children) { 17 // NSLog(@"studentelement %@", studentElement); 18 Student *student = [[Student alloc] init]; 19 // 遍历子节点的子节点 20 for (GDataXMLElement *stuElement in studentElement.children) { 21 // NSLog(@"stuelement %@", stuElement); 22 23 // 根据标签给student对象赋值 24 // stuElement.name 标签的名字 25 // stuElement.stringValue 标签的值 26 // KVC赋值 27 [student setValue:stuElement.stringValue forKey:stuElement.name]; 28 } 29 [self.dataArray addObject:student]; 30 } 31 // 遍历检验 32 for (Student *stu in self.dataArray) { 33 NSLog(@"name = %@ gender = %@ age = %ld hobby = %@", stu.name, stu.gender, stu.age, stu.hobby); 34 } 35 }
系统自带的JSON解析
1 #pragma mark - 系统自带的JSON数据解析 2 - (IBAction)foundationParserActionJSONDocument:(id)sender { 3 // 1.获取文件路径 4 NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentInfor_json" ofType:@"txt"]; 5 // 2.根据路径获取NSData 6 NSData *data = [NSData dataWithContentsOfFile:path]; 7 // 2.5 对存储数据的数组初始化 8 self.dataArray = [NSMutableArray array]; 9 // 3.解析 10 NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; 11 12 #pragma mark - jsonkit解析JSON数据解析 与系统自带的只差这一句 13 // NSArray *dataArray = [data objectFromJSONData]; 14 15 // 遍历数组 KVC赋值 16 for (NSDictionary *Dic in dataArray) { 17 Student *stu = [[Student alloc] init]; 18 // 将字典的值赋值给对象 19 [stu setValuesForKeysWithDictionary:Dic]; 20 // 将对象添加到数组中 21 [self.dataArray addObject:stu]; 22 } 23 for (Student *stu in self.dataArray) { 24 NSLog(@"name = %@ gender = %@ age = %ld hobby = %@", stu.name, stu.gender, stu.age, stu.hobby); 25 } 26 }
GET异步请求
#pragma mark - get 异步请求 - (IBAction)getRequest:(id)sender { // Block 实现 // 创建url NSURL *url = [NSURL URLWithString:GET_URL]; // 创建session对象 NSURLSession *session = [NSURLSession sharedSession]; // 创建task请求任务 // NSURLSession 是基于任务去完成相关的事件的 // NSURLSessionTask 所有的任务均放在这个里面实现 NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 解析相关的数据 if (nil == error) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@",dic); } }]; // 核心步:启动任务 (千万不能忘记) // NSURLSessionTask 实出来后处于挂起状态,如果不启动他永远不会走Block中的实现内容 [task resume]; }
POST异步请求
1 #pragma mark - post 异步请求 2 - (IBAction)postRequest:(id)sender { 3 // 创建url 4 NSURL *url = [NSURL URLWithString:POST_URL]; 5 // 创建请求 6 NSMutableURLRequest *mutableRequset = [NSMutableURLRequest requestWithURL:url]; 7 // 核心设置body 8 NSString *bodyString = POST_BODY; 9 NSData *postData = [bodyString dataUsingEncoding:NSUTF8StringEncoding]; 10 [mutableRequset setHTTPMethod:@"POST"]; 11 [mutableRequset setHTTPBody:postData]; 12 13 // 创建session 14 NSURLSession *session = [NSURLSession sharedSession]; 15 NSURLSessionDataTask *task = [session dataTaskWithRequest:mutableRequset completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 16 // 解析 17 if (nil == error) { 18 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; 19 NSLog(@"%@", dic); 20 } 21 }]; 22 // 启动任务 23 [task resume]; 24 }