9.0banb以前和9.0以后版本后JSON解析

 //9.0以前的老方法:同步请求、异步请求

    

    //1.获取访问路径

    NSString *path=@"http://1.studyios.sinaapp.com/getAllClass.php";

    //2.封装URL

    NSURL *url=[NSURL URLWithString:path];

    //3.创建请求命令

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    //4.响应的对象

    __autoreleasing NSURLResponse *response;

    //5.错误信息

    __autoreleasing NSError *err;

    //6.通过同步请求的方式 返回data对象

    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    //7.json解析

    NSArray *arrJson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&err];

    NSLog(@"%@",arrJson);

     

    

    //9.0以后

    //1.获取访问路径

    NSString *path=@"http://1.studyios.sinaapp.com/getAllClass.php";

    //2.封装URL

    NSURL *url=[NSURL URLWithString:path];

    //3.创建请求命令

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    //4.创建会话对象   通过单例方法实现的

    NSURLSession *session=[NSURLSession sharedSession];

    //5。实行回话任务 通过request请求  获取data对象

    NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

       //6、json 解析

        NSArray *arrJson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

        NSLog(@"%@",arrJson);

    }];

//    7、真正的执行任务

    [task resume];

   

}

原文地址:https://www.cnblogs.com/wrzheng/p/5317361.html