我有一个视图控制器,它包含一个标签和一个按钮。
我尝试过get request方法来解析数据。
@" http://adaptiveufest.appspot.com/getAllTimings?companyKey=457f0e98-1cd8-44cd-94a3-a3109a3a64a4"
我试过这个链接,但它没有用。
我需要发出get请求并解析来自此站点的数据,并在视图控制器标签中显示结果对象。
任何人都可以帮助我,我是初学者。
答案 0 :(得分:2)
Karthick Samy非常简单地可以在.m(实施)部分中应用以下代码
//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
答案 1 :(得分:1)
您可以使用Apple去年推出的最新NSURLSession
库以及iOS 7.浏览NSURLSession Tutorial以了解它的易用性。网络上有更多可用的帮助。
这里有一个快速举例说明第一个关键词'来自JSON数据的值 -
- (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];
}
答案 2 :(得分:0)
NSURLRequest
来获取请求。
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);
}
}
}
});
您也可以使用link