我有一个iOS应用程序设置为下载和解析JSON供稿。它可以很好地获取数据,但我正在努力阅读如何阅读Feed的某些嵌套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"];
感谢您的帮助,丹。
我在您的JSON转储中无法看到“display_location”或“country”。可能想要粘贴它的所有相关部分。 – DrummerB
@DrummerB我现在用完整的代码更新了这篇文章。 – Supertecnoboff
它在哪里失败?在第一行放置一个断点并逐步完成。 – DrummerB