afnetworking json成多维数组

时间:2012-03-21 21:10:52

标签: objective-c ios afnetworking

真的很想弄明白这一点。只是试图从PHP中获取数据

curRow = [[NSMutableArray alloc]init];
    [curRow addObject:[JSON valueForKeyPath:@"question"]];
    [curRow addObject:[JSON valueForKeyPath:@"answers"]];
    [curRow addObject:[JSON valueForKeyPath:@"correct_answer"]];
    [gameQuestions addObject:curRow];

在另一种方法中,我正在做这个

int i = arc4random() % [gameQuestions count];
CCLOG(@"Random: %i", i);

currentQuestion = [[gameQuestions objectAtIndex:i]objectAtIndex:0];
currentAnswer = [[gameQuestions objectAtIndex:i]objectAtIndex:1];
currentCorrectAnswer = [[gameQuestions objectAtIndex:i]objectAtIndex:2];
CCLOG(@"question: %@", currentQuestion);
CCLOG(@"answer: %@", currentAnswer);

但是当我查看调试日志时,我的问题和答案是同一个对象吗?

2012-03-21 17:08:06.659 dunce[6849:707] Random: 0
2012-03-21 17:08:06.662 dunce[6849:707] question: (
    "Who was the 42nd president?",
    "What was the date that Microsoft released Windows 95?"
)
2012-03-21 17:08:06.666 dunce[6849:707] answer: (
    "Bill Clinton; Theodore Rosevelt; Barack Obama; Ronald Regan; ",
    "June 25th, 1994;September 3rd, 1992; August 24th, 1995; August 3rd, 1996"
)

我必须遗漏某些东西

在php中它只是一个简单的while循环

while($row = mysql_fetch_array($result))
{
    $resultArr[$co]['id'] = $row['id'];
    $resultArr[$co]['question'] = $row['question'];
    $resultArr[$co]['answers'] = $row['answers'];
    $resultArr[$co]['correct_answer'] = $row['correct_answer'];
    $co++;
}

echo json_encode($resultArr);

任何想法?

1 个答案:

答案 0 :(得分:5)

我明白了。

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    for(id entry in JSON)
    {
        NSMutableArray *curRow = [[NSMutableArray alloc]init];
        [curRow addObject:[entry valueForKeyPath:@"question"]];
        [curRow addObject:[entry valueForKeyPath:@"answers"]];
        [curRow addObject:[entry valueForKeyPath:@"correct_answer"]];
        [gameQuestions addObject:curRow];
        [curRow release];
        curRow = nil;            
    }