从JSON文件iPhone中读取对象

时间:2012-08-26 09:36:39

标签: objective-c ios cocoa-touch

我用这种方式读了一个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"]}

为什么?

1 个答案:

答案 0 :(得分:0)

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