[self asynGETJSONWithURL:@"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" completion:^(id result) {
NSLog(@"%@", result[@"city"]);
}];
- (void)asynGETJSONWithURL:(NSString *)urlString completion:(void(^)(id result))block {
NSString *enCodingStr = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:enCodingStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *temResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (temResult.length == 0) {
return ;
}
temResult = [temResult substringFromIndex:21];
temResult = [temResult substringToIndex:temResult.length - 1];
NSData* xmlData = [temResult dataUsingEncoding:NSUTF8StringEncoding];
id result = [NSJSONSerialization JSONObjectWithData:xmlData options:NSJSONReadingMutableContainers error:&error];
block(result);
}];
[task resume];
}