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()
});
}
}
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
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With