1 #pragma mark 原生加载JSON 2 - (void)loadJSON 3 { 4 // 1. NSURL 5 NSURL *url = [NSURL URLWithString:@""]; 6 7 // 2. NSURLRequest 8 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 9 10 // 3. NSURLConnection 11 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 12 13 if (connectionError != nil) { 14 NSLog(@"%@", connectionError.localizedDescription); 15 } else { 16 NSError *error = nil; 17 18 NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; 19 20 if (error != nil) { 21 NSLog(@"%@", error.localizedDescription); 22 } else { 23 NSLog(@"%@", array); 24 } 25 } 26 }]; 27 } 28 #pragma mark AFN加载JSON 29 - (void)loadJSON 30 { 31 // 1. NSURLRequest 32 NSURLRequest *request = [_httpClient requestWithMethod:@"GET" path:@"videos.php?format=json" parameters:nil]; 33 34 // 2. 连接 35 AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSArray *JSON) { 36 37 // 成功之后,直接使用反序列化出来的结果即可。 38 NSLog(@"%@", JSON); 39 } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 40 // 1> 网络访问获取数据出错 41 // 2> JSON的反序列化出错 42 43 NSLog(@"%@ %@", error, JSON); 44 }]; 45 46 // 3. 将操作添加到队列或者start,操作就会启动 47 // 3.1 将操作添加到队列,开始多线程操作 48 [_httpClient.operationQueue addOperation:op]; 49 // [op start]; 50 }