以前从未接触过json。我正在尝试访问墨尔本Wunderground 天气 API 中的一些变量。例如,假设我想访问 "wind_dir":"East" 变量。到目前为止,这是我的代码:
NSString *urlString =
[NSString stringWithFormat:
@"http://api.wunderground.com/api/key/geolookup/conditions/forecast/q/-33.957550,151.230850.json"];
NSLog(@"URL = %@", urlString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *weatherInfo = [parser objectWithString:json_string error:nil];
for (NSDictionary *weatherString in weatherInfo)
{
NSLog(@"some weather info = %@", [[[weatherString objectForKey:@"response"] objectForKey:@"current_observation"] objectForKey:@"wind_dir"]);
}
我的代码到达 for 循环并因以下错误而崩溃:-[NSCFString objectForKey:]: unrecognized selector sent to instance.
我不是 100% 确定导致崩溃的原因,以及我的“wind_dir”变量路径是否正确,尽管它们很可能是同一个问题。
提前感谢您的帮助。