我正在尝试遍历 JSON 数组,但我不确定在这种情况下如何正确构建 each,有什么想法吗?
这是 $.get 得到的 JSON:
[{"Question":"Write your question here",
"Answers":
[{"Answers":"asd",
"Correct":false},
{"Answers":"dasdas",
"Correct":true
}
]},
{"Question":"Write your question here",
"Answers":
[{"Answers":"asdasd",
"Correct":false
}
]
}]
这是jQuery:
$.get("data.php", function(data){
var data = data.replace(/[\[\]']+/g, '')
$.each(data, function(i, q) {
var q = new Question(count++, data.Question);
$.each(data, function(i, val) {
q.addAnswer(data.Answers, Correct, q);
});
});
questions.push(q);
});
编辑:
$.get("data.php", function(data){
$.each(data, function(i, val) {
var q = new Question(count++, val.Question);
questions.push(q);
});
$.each(q.Answers, function(i, val) {
q.addAnswer(val, val.Correct, q);
questions.push(q);
});
});