如何访问JSON对象属性

How to access JSON object properties

本文关键字:JSON 对象 属性 访问 何访问      更新时间:2023-09-26

我无法访问JSON对象属性。给定这个构造函数

contractorTestQuestion = function (id, question, multianswer, textanswer, order) {
    var cntrQuestion;
    this.initialize(id, question, multianswer, textanswer, order);
}
$.extend(contractorTestQuestion.prototype, {
    initialize: function (_i, _q, _m, _t, _o) {
          cntrQuestion = {
            "questid" : _i,
            "question": _q,
            "multianswer": _m,
            "textanswer": _t,
            "order": _o,
            "answerswers": [],
            "change": 'none'
        }
    },
    addAnswer: function (a) {
        answers.push(a);
    },
    getAnswer: function (idx) {
        return this.answers[idx];
    }
});

然后在我的代码中像这样创建它:var cq = new contractorTestQuestion(qid, q, m, t, tqQuesPos);

我要做的是cq。问题,并能够获得存储在该字段中的值。

我不知道我做错了什么

不能通过将contractorTestQuestion添加到私有变量中来设置其属性。

this上设置如下属性:

initialize: function (_i, _q, _m, _t, _o) {
    this.questid = _i;
    this.question = _q;
    this.multianswer = _m;
    this.textanswer = _t;
    this.order = _o;
    this.answers = [];
    this.change = 'none';
},