0

我以这种方式读取了一个 JSON 文件:

NSString *fileContentHomework = [[NSString alloc] initWithContentsOfFile:homeworkPath];

SBJsonParser *parserHomework = [[SBJsonParser alloc] init];

NSDictionary *dataHomework = (NSDictionary *) [parserHomework objectWithString:fileContentHomework error:nil];

NSArray *nameofHomework = [dataHomework objectForKey:@"nameH"];
NSArray *homeworkDifficulty = [dataHomework objectForKey:@"difficultyH"];

CHOrderedDictionary* DictionaryHomework = [CHOrderedDictionary dictionaryWithObjects:homeworkDifficulty forKeys:nameofHomework];

...然后...

NSArray *allKeys2 = [DictionaryHomework allKeys];


        for (int i = 0; i < [allKeys count]; i++) {

……

NSString *key2 = [allKeys2 objectAtIndex:i];

            NSObject *obj2 = [DictionaryHomework objectForKey:key2];

            int val;
            val = [key2 intValue];

……

JSON 文件如下所示:

{"difficultyH":["name", "name"],"nameH":["2", "2"]}

我注意到,如果文件中的数据相同,如上所示,应用程序会给我一个 objectAtIndex 错误,而如果数据不同,如以下情况,应用程序工作正常:

{"difficultyH":["name", "name2"],"nameH":["2", "3"]}

为什么?

4

1 回答 1

0

它与 JSON 无关。在第一种情况下,您尝试使用重复键构建 NSDictionary。那是行不通的。

于 2012-08-26T14:18:25.333 回答