系统提供的方式
NSString *urlString = @"https://xxx.xxx.xxx/xxx";
NSURLSession *sharedSession = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [sharedSession dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSError *jsonError;
NSDictionary *jsonObj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
NSArray *arr = [jsonObj allKeys];
for (int i = 0; i < jsonObj.count; i++) {
NSLog(@"%@: %@", arr[i], [jsonObj objectForKey:arr[i]]);
}
}];
[dataTask resume];
第三方库方式
NSString *urlString = @"https://xxx.xxx.xxx/xxx";
[[AFHTTPSessionManager manager]GET:urlString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}
];