我有一个 iOS 应用程序设置为下载和解析 JSON 提要。它可以很好地获取数据,但我正在努力研究如何读取提要的某些嵌套 JSON 数据。
这是我要加载的 JSON 提要:http: //api.wunderground.com/api/595007cb79ada1b1/conditions/forecast/q/51.5171,0.1062.json
我尝试加载的 JSON 提要部分是:
"forecast":{
"txt_forecast": {
"date":"1:00 AM BST",
"forecastday": [
{
"period":0,
"icon":"chancerain",
"icon_url":"http://icons-ak.wxug.com/i/c/k/chancerain.gif",
"title":"Thursday",
"fcttext":"Clear with a chance of rain in the morning, then mostly cloudy with a chance of rain. High of 59F. Winds from the SSE at 5 to 10 mph. Chance of rain 60%.",
"fcttext_metric":"Clear with a chance of rain in the morning, then mostly cloudy with a chance of rain. High of 15C. Winds from the SSE at 5 to 15 km/h. Chance of rain 60%.",
"pop":"60"
}
你看到有一个叫做“句号”的位:0 在句号 0 中有“图标”,“标题”等......
好吧,我正在尝试访问该数据,但我不能。
这是我用于访问嵌套 JSON 提要的特定部分的代码:
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError];
NSArray *results = [res objectForKey:@"current_observation"];
NSArray *cur = [results valueForKey:@"weather"];
NSArray *windrep = [results valueForKey:@"wind_string"];
NSArray *UVrep = [results valueForKey:@"UV"];
NSArray *othertemprep = [results valueForKey:@"temperature_string"];
NSString *loc = [[results valueForKey:@"display_location"] valueForKey:@"city"];
NSString *countcode = [[results valueForKey:@"display_location"] valueForKey:@"country"];
我怎样才能改变它来访问我想要的东西?
这是我的另一个尝试:
NSString *test = [[[results valueForKey:@"forecast"] valueForKey:@"period:0"] valueForKey:@"title"];
谢谢你的帮助,丹。