2012-03-21 14 views
3

これを理解するのは本当に苦労します。ちょうど別の方法では、PHPafnetworking jsonを多次元配列に変換

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); 

任意のアイデア?

+0

問題を解決してください。あなたのPHPスクリプトは、期待通りに構造化されたデータを持つ正しくエンコードされたJSONを生成しているようですか? Objective-C側では、さまざまな場所でデバッガを使用して、さまざまな配列に割り当てるデータが実際に意味を持つかどうかを確認します。 100回中99回、何かを一歩一歩踏み出し、実際に何が起こっているのかを実際に調べると、自分の問題を解決することができます。 – warrenm

+0

私は知っています。問題は、どこに問題があるかを知ることです。 PHPから来て、Obj-Cは私とは違う世界です。しかし、私はしようとしています... – lsiunsuex

+0

私は私がどのように私はPHPでそれを解決するだろうと思うしようとするすべての問題をobj - cに変換します。私は結果を反復しなければならないことを知っていたはずです。ちょうどafnetworkingが何らかの理由で私にそのようにそれを与えると思った。 – lsiunsuex

答えて

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;    
    } 
+0

偉大な、これは私が多くの助け:) – Charles