如何在javascript中为json对象创建方法?

时间:2015-05-15 18:20:58

标签: javascript json methods javascript-objects

我已经创建了一个测验,其中每个问题都是一个问题对象,该问题包含打印静止等方法。我在javascript文件中添加了我的问题,但我想将问题移到外部Json文件中。

但是,在这种情况下,我无法找到任何文章介绍如何为导入的Json对象(问题对象)创建两个方法。在使用getJson:

之前,这是测试对象的一段代码,其中包含一个方法
$(function(){

// <-- QUESTION OBJECT -->
function Question(question, choices, correctChoice, userAnswer, type) {

    this.question = question;
    this.choices = choices;
    this.correctChoice = correctChoice;
    this.userAnswer = userAnswer;
    this.type = type;   // either radio buttons or check boxes

    // <-- write question method -->
    this.writeQuestion = function() {

        var questionContent;
        questionContent = "<p class='question'>" + (currentQue + 1) + ". " +    this.question + "</p>";
        var checked = "";

        if(this.type === "radio") {

            for(var i=0; i < this.choices.length; i++) {

                if((i+1) == this.userAnswer)
                    checked = "checked='checked'";

                questionContent += "<p><input class='css-radio' id='radio" + (i+1) + "' " +  checked  + " type='radio' name='choices' value='choice'></input>";
                questionContent += "<label class='css-label' for='radio" + (i+1) + "'>" + this.choices[i] + "</label></p>"; 

                checked = "";
            }
        }

        else {

            for(var i=0; i < this.choices.length; i++) {

                if ((i+1) == this.userAnswer[i])
                    checked = "checked='checked'";

                questionContent += "<p><input class='css-checkbox' id='checkbox" + (i+1) + "' " +  checked  + " type='checkbox' name='choices' value='choice'></input>";
                questionContent += "<label class='css-label-checkbox' for='checkbox" + (i+1) + "'>" + this.choices[i] + "</label></p>";

                checked = "";
            }
        }

    return $quizContent.html(questionContent);
    };

2 个答案:

答案 0 :(得分:1)

你应该创建一个接收json的构造函数,并使用你提供的json定义那里的所有方法,这样粗略:

function Question(questionJson) {
    var data = questionJson;

    //Public "methods" are defined to "this"
    this.getCorrectAnswer = function() {
        return data.correctAnswer;
    };

    //Private "methods
    var doSomethingCrazy = function() {
        return "crazy";
    };

    this.getSomethingCrazy = function() {
        return doSomethingCrazy();
    };
}

然后,让我们说你有一系列问题:

var questions = [
    {questionId: '1', correctAnswer: 'a', possibleAnswers: []},
    {questionId: '2', correctAnswer: 'a', possibleAnswers: []},
];

你这样做:

var instances = [];
for (var q in questions) {
    instances[q.questionId] = new Question(q);
}

答案 1 :(得分:0)

此代码有效:

var allQuestions = [];
var category = "history";

for(var i=0; i < jsonDataLength; i++) {

    allQuestions[i] = new Question(); // create empty question Obj
    var questionNr = "q" + (i+1).toString(); // store questionNr in variable

    for(var properties in jsonData[category][questionNr]) // loop through questionObjs properties
        allQuestions[i][properties] = jsonData[category][questionNr][properties]; 
                     // add them to empty QuestionObj
}