JSON解析之NSJSONSerialization
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//获取url路径
NSURL *url = [NSURL URLWithString:@"http://localhost/demo.json"];
//单例session
NSURLSession *session = [NSURLSession sharedSession];
//发起任务
NSURLSessionDataTask *datatask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//处理错误信息
if(error == nil && data != nil)
{
//json反序列化
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//可在此处进行字典数组转模型
NSLog(@"%@--%@",[result class],result);
}else
{
NSLog(@"%@",error);
}
}];
//启动任务
[datatask resume];
}