So, I have a JSON file from which I need to get Quiz questions. At the moment, I'm just storing questions in an array as an object. Each question object has a "text" (the question itself), a "choice" (an array of possible answers) and an "answer" (int corresponding to the location of the correct choice of answer).
How can I verify that I am storing question objects correctly? I want to create a list of questions, and I tried to populate my list with [i] .text questions, and that didn't work. I installed Firebug to debug what is happening, but I'm not quite sure how to make the best use of it.
JSON is in this format:
{ "text": "What does the author least like about Eclipse?", "choices": [ "The plugin architecture.", "FindBugs.", "Refactoring.", "The Run As menu."], "answer": 3 }
My JavaScript file:
$(document).ready(function(){ var questions=[]; $.getJSON('quiz.js',function(data){ var i=0; for(i=0;i<data.length;i++){ questions[i]=[String(data[i].text),String(data[i].choices),int(data[i].answer)]; } var list = $('#list') $(questions).each(function(_, text) { var item = $('<li/>') var link = $('<a/>').html(text) link.click(function() { alert(text) }) item.append(link) list.append(item) }) $('#list').listview('refresh') }); })
Finally, some HTML:
<div data-role="content"> <ul id="list" data-role="listview"> </ul> </div>
I know this is a long question, but I really appreciate any help. The ultimate goal is to have a list of questions that when clicked displays the answer options and gives a toast to notify the user if the selected option is correct or not. I also want to highlight the question in the list in "Green" if you answer correctly, and Red - otherwise.
EDIT:
Work code:
$(document).ready(function(){ $.getJSON('quiz.js',function(data){ var questions = data; var list = $('#list') $(questions).each(function(index, object) { $.each(object, function(key, value){ if(key === 'text'){