Ios 在xcode中获取请求调用

Ios 在xcode中获取请求调用,ios,xcode,Ios,Xcode,我有一个视图控制器,它包含一个标签和一个按钮 我尝试了get请求方法来解析数据 @ 我尝试了这个链接,但它不起作用 我需要发出get请求并解析来自此站点的数据,并在视图控制器标签中显示结果对象 有谁能帮我一下吗?我是个初学者。NSURLRequest在Objective中用于发出get请求 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSURLRequest *re

我有一个视图控制器,它包含一个标签和一个按钮

我尝试了get请求方法来解析数据

@

我尝试了这个链接,但它不起作用

我需要发出get请求并解析来自此站点的数据,并在视图控制器标签中显示结果对象

有谁能帮我一下吗?我是个初学者。

NSURLRequest在Objective中用于发出get请求

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://adaptiveufest.appspot.com/getAllTimings?companyKey=457f0e98-1cd8-44cd-94a3-a3109a3a64a4"]];
    NSError * error = nil;
    NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    if (error == nil)
    {
        NSString *returnObject = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[returnObject dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
        for (NSDictionary *val in dict) {
            NSLog(@"colorcode : %@",[val objectForKey:@"colorcode"]);
            NSLog(@"key : %@",[val objectForKey:@"key"]);
            NSLog(@"id : %@",[val objectForKey:@"id"]);
            NSLog(@"serviceName : %@",[val objectForKey:@"serviceName"]);
            NSLog(@"status : %@",[val objectForKey:@"status"]);
            NSLog(@"startDateString : %@",[val objectForKey:@"startDateString"]);
            NSLog(@"endDateString : %@",[val objectForKey:@"endDateString"]);
            NSLog(@"serviceCost : %@",[val objectForKey:@"serviceCost"]);
            NSLog(@"f_Key : %@",[val objectForKey:@"f_Key"]);
            NSArray *providerAr = [NSArray arrayWithArray:[val objectForKey:@"providersList"]];
            for (NSString*valStr in providerAr) {
                NSLog(@"providerVal : %@",valStr);
            }
        }
    }
});
您还可以使用

您可以使用最新的NSURLSession库,该库是苹果去年与iOS 7一起推出的。去学习一下它的易用性。同样的,在网上有更多的帮助

下面是一个从JSON数据中获取第一个“key”值的快速示例-

- (void)getData {

    NSURLSession *session = [NSURLSession sharedSession];

    NSString *urlString = [[NSString alloc] initWithFormat:@"http://adaptiveufest.appspot.com/getAllTimings?companyKey=457f0e98-1cd8-44cd-94a3-a3109a3a64a4"];

    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

        NSData *data = [[NSData alloc] initWithContentsOfURL:location];
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        // Do mapping of objects 
        self.keyString = [[responseDictionary valueForKey:@"key"] objectAtIndex:0];

        dispatch_async(dispatch_get_main_queue(), ^{
            // Update value on main thread (UILabel)
            NSLog(@"Value for the first key: %@", self.keyString);
        });
    }];
    [task resume];
}

Karthick Samy非常简单,您可以在.mi实现部分应用以下代码

  //just give your URL instead of my URL

  NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL     URLWithString:@"http://api.worldweatheronline.com/free/v1/search.ashx?query=London&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]];

  [request setHTTPMethod:@"GET"];

  [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

  NSError *err;

  NSURLResponse *response;

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

 //You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER.If you do this clearly you can get the correct results.    

 //After that it depends upon the json format whether it is DICTIONARY or ARRAY 

  NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

  NSArray *array=[[jsonArray objectForKey:@"search_api"]objectForKey:@"result"]; // For Example i give the key is search_api.So according to your response just give your exact key.You must give the correct key.Otherwise it wont give result

karthik,网络上有几个教程,只要试着问一下你的问题就可以了