首页 > 解决方案 > 通过 jquery 提取 JSON 数据的问题

问题描述

我正在尝试使用 $.getJSON 方法提取一些 json 数据,但它对我不起作用。我对 js 很陌生,所以任何帮助都将不胜感激。请在下面找到json。

[
   {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:1},
   {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:3},
  {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:2},
  {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:1},
   {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:1}

  ];

在这里你可以找到脚本文件

$(document).ready(function() {
      var total_questions = 5;
      var questions = [];
      var questionNum = 0;
      var questionTotal = questions.length;
      var correctTotal = 0;
      $('#testQuestion').hide();
      $('#startQuizButton').click(function() {
        $('#message').hide();
        $('#startQuiz').hide();
        $('#testQuestion').show();
        $.getJSON('questions.json', function(data) {
          $.each(data, function(index, item) {
            $("#question").append(item);
          });
        });
        questionDisplay();
      });

      function questionDisplay() {
        $('#questionNum').text("Question " + (questionNum + 1) + " of " +
          total_questions);
        $('#question').text(questions[questionNum].question);
        $('#choices').empty();
        var choiceTotal = questions[questionNum].choices.length;
        for (var i = 0; i < choiceTotal; i++) { //displays the answer 
          choices
          $('#choices').append("<input type='radio' class='guess' name='guess' 
            value = " + i + " > " + questions[questionNum].choices[i] + " < br > ");
          }
        }
      })

标签: javascriptjson

解决方案


推荐阅读