Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JSON with multilevel

How is the multilevel JSON set up in this case?

var quiz = {};

for(var i = 0; i < questions.length; i++) {     
quiz['Questions'] = {
    Question: Question,
    Answers: Answers,
    Correct: Correct
    };
    };

json_quizModule = JSON.stringify({QuizModule: quiz }, null, "\t");
alert(json_quizModule)

Now all of it is under the parameter "Question". But I trying the achieve this structure of a JSON:

{
"QuizModule": {
    "Questions": {
        "Question": "Write your question here" 
                                 { "Answers": "sds" 
                                          { "Correct": false
                                          }
                                 }
    }

}
}

I'm quite new to JSON, and can there also be several parameters names "Question", in case there are multiple questions?

Really thankful for some help here!

UPDATE:

Jquery:

    var quiz = {};

    var quiz = {
    Questions: []
    };

    for(var i = 0; i < questions.length; i++) {
    var q = questions[i];
    var answers = q.getAnswers()
    quiz.Questions.push({
    Question: q.getQuestion()
    });

    for(var n = 0; n < answers.length; n++){

    var quiz = {
    Answers: []
    };

    quiz.Questions.Answers.push({       
        Answers: answers[n].getAnswer(),
        Correct: answers[n].getCorrect()
     });

     } 
    }
like image 399
Kim Avatar asked Feb 01 '26 20:02

Kim


1 Answers

To have more than one Question section you need the following structure

"QuizModule": {
    "Questions": [
        "Question": "Write your question here" {
        "Answers": "sds" {
            "Correct": false
        }

    },
        "Question": "Write your question here" {
        "Answers": "sds" {
            "Correct": false
        }]

    }
}

notice [ instead of { . [ is used to indicate array

It can be achieved using the push method like

for (var i = 0; i < questions.length; i++) {
    quiz['Questions'].push({
        Question: Question,
        Answers: Answers,
        Correct: Correct
    });
};
like image 119
U.P Avatar answered Feb 03 '26 10:02

U.P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!