我刚开始使用Objective-C,但似乎无法提取AFNetworking拉取的数据作为标签文本等。我将其用于由Forecast.io和Forecastr应用编程接口包装支持的天气应用程序。如果API将JSON写入字典,我似乎找不到它。
在主视图控制器中:
[forecastr getForecastForLocation:location time:nil exclusions:nil success:^(id JSON) {
NSLog(@"JSON response was: %@", JSON);
NSLog(@"Testing: %@", [JSON valueForKey:kFCSummary]);
} failure:^(NSError *error, id response) {
NSLog(@"Error while retrieving forecast: %@", [forecastr messageForError:error withResponse:response]);
}];
}第一行NSLog将返回完整的JSON对象、序列化的内容以及所有内容,但第二行只返回NULL。
在Forecast.m文件中:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
if (self.callback) {
AFHTTPRequestOperation *httpOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *JSONP = [[NSString alloc] initWithData:responseObject encoding:NSASCIIStringEncoding];
if (self.cacheEnabled) [self cacheForecast:JSONP withURLString:cacheKey];
success(JSONP);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error, operation);
}];
[pendingRequests addObject:httpOperation];
[forecastrQueue addOperation:httpOperation];
} else {
AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if (self.cacheEnabled) [self cacheForecast:JSON withURLString:cacheKey];
success(JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
failure(error, JSON);
}];
[pendingRequests addObject:jsonOperation];
[forecastrQueue addOperation:jsonOperation];
}我可以判断缓存预测的读数,如果缓存太旧,就会拉下一个新的JSON文件。但我不知道它把JSON放在哪里,而且它也不在大多数教程和资源使用的字典中。
我已经尝试了很多不同的方法,比如创建我自己的字典,但它不会在其中添加JSON对象。我还尝试将返回的JSON对象用作字典和键值对,但似乎都不起作用。几天来,我一直在用头撞墙,但几乎没有人使用Forecastr来解决问题。
https://stackoverflow.com/questions/17130212
复制相似问题