我的项目有一些问题。所以我使用 JSON 来获取一些信息。当我有带有文件夹的 JSON 时(例如:我在这个播放列表中有名称为 Playlist 的 NSDictionary,我有 NSString 名称和专辑)为此我编写了以下代码:
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSDictionary *playlist =[allDataDictionary objectForKey:@"playlist"];
for (NSDictionary *diction in playlist) {
NSDictionary *artist = [diction objectForKey:@"artist"];
NSDictionary *song = [diction objectForKey:@"song"];
NSString *name = [artist objectForKey:@"name"];
NSString *namesong = [song objectForKey:@"name"];
[array addObject:name];
[array2 addObject:namesong];
}
[[self tableTrack]reloadData];
}
这是完美的工作!但!当我没有任何文件夹时,只有没有 NSDictionary 的 JSON,只有 NSStrings,如何制作?对不起我的愚蠢问题,但我真的试着写:
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSString *name = [allDataDictionary objectForKey:@"name"];
NSString *namesong = [allDataDictionary objectForKey:@"name"];
[array addObject:name];
[array2 addObject:namesong];
}
[[self tableTrack]reloadData];
}
但我有错误,我的应用程序也崩溃了:
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7615c90”
那么,我做错了什么?