actionscript 3-AS3 XML到多维数组



我到处都找过,虽然有很多解释,但我似乎无法理解。

这是我的xml结构:

 <question> 
            <q1> Who coined the term “Clinical Psychology”? </q1>       
            <answer> Lightner Witmer </answer>
            <option1> Stanley Hall </option1>
            <option2> Lightner Witmer </option2>
            <option3> Henry P. David </option3>
    </question>

我可以循环浏览fine,找出问题和答案,然后将它们放入单独的数组中。我遇到的问题是循环并将选项拉入多维数组,比如

var one:Array = new Array( 3 ); 
one[0] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[1] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[2] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];

如有任何帮助,我们将不胜感激。

    var answers:Array = [];
    for(var i = 0; i< xml.question.length; i++){
    var node:Array = [];
    node.push(xml.question[i].option1);
    node.push(xml.question[i].option2);
    node.push(xml.question[i].option3);
//or
    var node:Array = [xml.question[i].option1,xml.question[i].option2,xml.question[i].option3];
    answers.push(node);
    }

现在可以检索问题的答案:

answers[questionIndex][answerIndex];

希望对有所帮助

我不确定你是否以你想要的形式设置了数组,但是,偏离了你发布的代码
你试过了吗?

var one:Array = new Array( 3 ); 
one[0] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[1] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[2] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );



有了这样的东西,我更喜欢有一个以对象为元素的问题数组。每个对象都将包含有关该问题的所有信息。这将允许为每个问题分配一个自定义类。或者可以使用基本的动态对象

var questions:Array = new Array();
var obj:Object = new Object();
obj.question = "Who coined the term 'Clinical Psychology'?"
obj.correctAnswer = "Lightner Witmer";
obj.possibleAnswers =new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
questions.push( obj );

相关内容

  • 没有找到相关文章

最新更新