ios - 以前的 NSDictionary 现在到 JSON 数组

标签 ios objective-c json

- (void)retrieveData
{
    NSURL * url = [NSURL URLWithString:@"***/connection.php"];
    NSData * data = [NSData dataWithContentsOfURL:url];

    _json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    _questionsArray = [[NSMutableArray alloc]init];

    for (int i = 0; i < _json.count; i++)
    {
        NSString * qID = [[_json objectAtIndex:i] objectForKey:@"id"];
        NSString * qTitle = [[_json objectAtIndex:i] objectForKey:@"question_title"];
        NSString * qA = [[_json objectAtIndex:i] objectForKey:@"A"];
        NSString * qB = [[_json objectAtIndex:i] objectForKey:@"B"];
        NSString * qC = [[_json objectAtIndex:i] objectForKey:@"C"];
        NSString * qD = [[_json objectAtIndex:i] objectForKey:@"D"];
        NSString * qAnswer = [[_json objectAtIndex:i] objectForKey:@"question_answer"];

        question * myQuestion = [[question alloc] initWithQuestionID:qID andQuestionName:qTitle andQuestionA:qA andQuestionB:qB andQuestionC:qC andQuestionD:qD andQuestionAnswer:qAnswer];

        [_questionsArray addObject:myQuestion];
    }

    [_json enumerateObjectsUsingBlock:^(NSDictionary *questionDictionary, NSUInteger idx, BOOL *stop) {
        //Here I'm treating the index like an NSNumber, if your code is expecting a string instead use
        //@(idx).stringValue
        [_questions setObject:questionDictionary forKey:@(idx)];

        //or the modern equivalent
        //questions[@(idx)] = questionDictionary;

        //If you want to use your 'questions class' then create one and put it into the array instead of the dictionary pulled from the array.
    }];

    NSLog( @"%@", _questions );

}

日志(空)

随机的 gobledy gook 所以我的帖子主要不是代码 随机 gobledy gook 所以我的帖子主要不是代码 随机 gobledy gook 所以我的帖子主要不是代码 随机 gobledy gook 所以我的帖子主要不是代码 随机的 gobledy gook 所以我的帖子主要不是代码

最佳答案

如果我正确理解你的问题,它会变成这样

self.questions = .... //I assume this is the array you reference 'question' objects that is created by your retrieve data method

//this used to be created by pulling an object out of your questions dictionary with the key i interpreted as a string.
//now that it's an array you should be able to just reference it by index, assuming they were inserted in order
//I'm also assuming that what comes out of the aray is a question object given the code you provided with the signature
//- (id) initWithQuestionID: (NSString *) qID andQuestionName: (NSString *) qName andQuestionA: (NSString *) qA andQuestionB: (NSString *) qB andQuestionC: (NSString *) qC andQuestionD: (NSString *) qD andQuestionAnswer: (NSString *) qAnswer
Question *nextQuestion = self.questions[i];


self.answer = nextQuestion.questionAnswer;
self.questionLabel.text = nextQuestion.questionLabel;
//and so on

我还建议进行以下编辑以替换您的 for 循环。它使用 for in 循环代替,这使您不必跟踪索引并且看起来更干净。它也有帮助,因此您不会一直重复 [_json objectAtIndex:i] 代码块。我还使用现代 objective-c 语法来访问字典。

for (NSDictionary *questionDictionary in _json)
{
    NSString * qID = questionDictionary[@"id"];
    NSString * qTitle = questionDictionary[@"question_title"];

    ...
    question * myQuestion = [[question alloc] initWithQuestionID:qID andQuestionName:qTitle andQuestionA:qA andQuestionB:qB andQuestionC:qC andQuestionD:qD andQuestionAnswer:qAnswer];
    [_questionsArray addObject:myQuestion];
}

如果您需要字典中的键和对象,那么您可以使用 enumerateObjectsUsingBlock 以类似的方式清理它

[_json enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    //your code here
}];

编辑

听起来您真正想做的是提取 JSON,但保留所有其他代码,就像您使用从 plist 中获取的字典时一样。所以在这种情况下,你希望你的解析函数返回一个字典而不是一个数组。如果是这样的话,值得暂时避开计算机科学。

NSDictionary 也称为散列、映射、符号表或关联数组。有些语言(比如 Lua)没有像 NSArray 这样的数组集合,它们只有字典。从字典中,您可以创建许多您过去喜欢数组(和集合)的其他集合。它是这样工作的:不是使用索引的有序元素集合,而是将项目放在字典中,并使用索引作为键,值就变成了值。例如一个数组及其等效的关联数组(又名字典):

NSArray *array = @[@"hello", @"world", @"!"];

NSDictionary *dictionary = @{@(1): @"hello",
                             @(2): @"world",
                             @(3): @"!"};

这正是您从 plist 加载数据时所做的,因为第一个元素键是 0,后跟另一个字典,我假设列表中的下一个元素是 1,后跟另一个字典。在你的解析函数中,它变成了

NSMutableDictionary *questions = [[NSMutableDictionary alloc] init];
[_json enumerateObjectsUsingBlock:^(NSDictionary *questionDictionary, NSUInteger idx, BOOL *stop) {
    //Here I'm treating the index like an NSNumber, if your code is expecting a string instead use
    //@(idx).stringValue
    [questions setObject:questionDictionary forKey:@(idx)];

    //or the modern equivalent
    //questions[@(idx)] = questionDictionary;

    //If you want to use your 'questions class' then create one and put it into the array instead of the dictionary pulled from the array.
}]; 

这当然假设您的 api 将按照您想要的顺序返回 JSON 问题。

关于ios - 以前的 NSDictionary 现在到 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24473241/

相关文章:

ios - Keychain Services API 在 iphonesimulator 6.0 中因 errSecNotAvailable 而失败

iphone - 架构 armv7 的 undefined symbol : for external C library

ios - iOS的社交身份验证和发布库

iOS Safari 位置 :fixed doesn't work at all

ios - 如何使用 App Bundle 之外的本地化资源

iphone - 为什么会出现 NSException 错误?

苹果手机 : how to stop shutter animation?

c++ - 将 unsigned char * 转换为 json,jsonspp C++

php - Xcode 7 beta swift 使用未解析的标识符

ios - 无法将已解析的 JSON 列表显示到 TableView 中