初学者的总结的网络请求数据的JSON的解析的两种方法对比,有什么不对的地方还请见谅
//(老方法)网络请求数据JSON解析
//获取访问路径
NSString *path=@"http://1.studyios.sinaapp.com/getAllClass.php";
//封装URl
NSURL *url=[NSURL URLWithString:path];
//创建请求命令
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//获取访问路径
NSString *path=@"http://1.studyios.sinaapp.com/getAllClass.php";
//封装URl
NSURL *url=[NSURL URLWithString:path];
//创建请求命令
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//响应的对象
__autoreleasing NSURLResponse *response;
//错误信息
__autoreleasing NSError *error;
//通过同步请求的方式返回data类型的对象
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSArray *arrjson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"%@",arrjson);
__autoreleasing NSError *error;
//通过同步请求的方式返回data类型的对象
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSArray *arrjson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"%@",arrjson);
//(新方法)网络请求数据JSON解析
//获取访问路径
NSString *path=@"http://1.studyios.sinaapp.com/getAllClass.php";
//封装URl
NSURL *url=[NSURL URLWithString:path];
//创建请求命令
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//创建会话对象,通过单例方法实现
NSURLSession *session=[NSURLSession sharedSession];
//执行会话的任务,通过request请求,获取data对象
NSURLSessionDataTask *task= [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
//json解析
NSArray *arrjson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"%@",arrjson);
}];
//代码块的方法回调
[task resume];