首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Forecastr包装器中的JSON结果

无法使用Forecastr包装器中的JSON结果
EN

Stack Overflow用户
提问于 2013-06-16 12:31:05
回答 2查看 248关注 0票数 1

我刚开始使用Objective-C,但似乎无法提取AFNetworking拉取的数据作为标签文本等。我将其用于由Forecast.io和Forecastr应用编程接口包装支持的天气应用程序。如果API将JSON写入字典,我似乎找不到它。

在主视图控制器中:

代码语言:javascript
复制
[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文件中:

代码语言:javascript
复制
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来解决问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-17 02:47:11

问题是,您要返回的数据被分成许多不同的部分(例如当天和一周中的每一天)。你不能仅仅询问温度的总体数据集,你需要导航到你感兴趣的“天”,然后请求温度。例如:

代码语言:javascript
复制
[[JSON objectForKey:kFCCurrentlyForecast] objectForKey:kFCTemperature]
票数 1
EN

Stack Overflow用户

发布于 2013-06-16 12:36:06

JSON似乎是一个字符串(github)。为了解析它,您可以使用诸如JSONKit之类的库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17130212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档