GDataXMLDocument-XML解析
#pragma mark - ======== Dom XML解析
- (IBAction)domParserActionXML_Document:(id)sender { // 第一步 引入动态库
// 1 获取文件路径
NSString *Path = [[NSBundle mainBundle] pathForResource:@"StudentIfor_xml" ofType:@"txt"];
// 2.根据路经获取data类型数据
NSData *data = [NSData dataWithContentsOfFile:Path];
// 2.1 初始化存储数据的数组
self.dataArray = [NSMutableArray array];
// 3.设置dom解析(创建解析文档)
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options: error:nil];
// 4.获取根节点
GDataXMLElement *rootElement = document.rootElement;
// 5.遍历获取相对应的子节点
for (GDataXMLElement *studentElement in rootElement.children) {
// NSLog(@"studentelement %@", studentElement);
Student *student = [[Student alloc] init];
// 遍历子节点的子节点
for (GDataXMLElement *stuElement in studentElement.children) {
// NSLog(@"stuelement %@", stuElement); // 根据标签给student对象赋值
// stuElement.name 标签的名字
// stuElement.stringValue 标签的值
// KVC赋值
[student setValue:stuElement.stringValue forKey:stuElement.name];
}
[self.dataArray addObject:student];
}
// 遍历检验
for (Student *stu in self.dataArray) {
NSLog(@"name = %@ gender = %@ age = %ld hobby = %@", stu.name, stu.gender, stu.age, stu.hobby);
}
}
系统自带的JSON解析
#pragma mark - 系统自带的JSON数据解析
- (IBAction)foundationParserActionJSONDocument:(id)sender {
// 1.获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentInfor_json" ofType:@"txt"];
// 2.根据路径获取NSData
NSData *data = [NSData dataWithContentsOfFile:path];
// 2.5 对存储数据的数组初始化
self.dataArray = [NSMutableArray array];
// 3.解析
NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; #pragma mark - jsonkit解析JSON数据解析 与系统自带的只差这一句
// NSArray *dataArray = [data objectFromJSONData]; // 遍历数组 KVC赋值
for (NSDictionary *Dic in dataArray) {
Student *stu = [[Student alloc] init];
// 将字典的值赋值给对象
[stu setValuesForKeysWithDictionary:Dic];
// 将对象添加到数组中
[self.dataArray addObject:stu];
}
for (Student *stu in self.dataArray) {
NSLog(@"name = %@ gender = %@ age = %ld hobby = %@", stu.name, stu.gender, stu.age, stu.hobby);
}
}
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异步请求
#pragma mark - post 异步请求
- (IBAction)postRequest:(id)sender {
// 创建url
NSURL *url = [NSURL URLWithString:POST_URL];
// 创建请求
NSMutableURLRequest *mutableRequset = [NSMutableURLRequest requestWithURL:url];
// 核心设置body
NSString *bodyString = POST_BODY;
NSData *postData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[mutableRequset setHTTPMethod:@"POST"];
[mutableRequset setHTTPBody:postData]; // 创建session
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:mutableRequset completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 解析
if (nil == error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dic);
}
}];
// 启动任务
[task resume];
}