获取到mysql的Json数据。 问题。在JSON数据中有opt1,opt2,opt3 ...... opt7。
下面我通过for循环获得opt1..7值,但我得到每个opt1 ... 7值undefined。为什么呢?
JSON数据:
{"qz_id":"15","qzn_id":"14","ins_user_id":"1","qnumber":"8","quiz_question":"jdfkjf dkfj dkjfkd kid jfkjdk fdkjfk djfkdjfkd kfkd fk djfkd jfk d",
"opt1":"jkj","opt2":"kjkj","opt3":"kjkj","opt4":"kjkjkjk","opt5":"","opt6":"","opt7":"","right_opt":"opt1","qns_explanation":"",
"Answeroption":"0","q_mark":"0","qns_neq_mark":"0",
"qns_DifficultyLevel":"1","cdate":"2017","stt":"In-active","del_stt":"No","quiz_name":"ssdsds demo","exam_id":"5","sec_id":"6"}
AJAX代码:
$.ajax({
url: "quiz_process.php",
type: "POST",
data: dataString,
cache: false,
success: function(data) {
var obj=JSON.parse(data);
/*opt a*/
var optv="";
var optionv="";
for(var i=1;i<=7;i++) {
optv='opt'+i;
optionv = optv+'::'+obj.optv;
console.log(optionv);
}
}
});
答案 0 :(得分:1)
如果要通过变量访问对象属性,则必须稍微更改语法:
optionv = optv+'::'+obj[optv];
否则,当您执行obj.optv
时,您尝试访问对象中不存在的optv
属性。