如何显示获取[对象对象]的Json数据?

时间:2015-10-15 05:11:19

标签: javascript jquery json each

$.getJSON('data.json', function (data) {
    $.each(data.questions, function (index, data) {
        console.log(data);
    });
}); // getJSON

Data JSON:

{
    "questions": [
        {
            "qset": {
                "q1": "Template 1 question1",
                "q2": "Template 1 question2",
                "q3": "Template 1 question3",
                "q4": "Template 1 question4",
                "q5": "Template 1 question5"
            }
        },
        {
            "qset": {
                "q1": "Template 2 question1",
                "q2": "Template 2 question2",
                "q3": "Template 2 question3",
                "q4": "Template 2 question4",
                "q5": "Template 2 question5"
            }
        },
    ]
}

2 个答案:

答案 0 :(得分:1)

只是验证你的json,然后尝试安装你的json properties

{
    "questions": [
        {
            "qset": {
                "q1": "Template 1 question1",
                "q2": "Template 1 question2",
                "q3": "Template 1 question3",
                "q4": "Template 1 question4",
                "q5": "Template 1 question5"
            }
        },
        {
            "qset": {
                "q1": "Template 2 question1",
                "q2": "Template 2 question2",
                "q3": "Template 2 question3",
                "q4": "Template 2 question4",
                "q5": "Template 2 question5"
            }
        } //remove comma
    ]
}

答案 1 :(得分:0)

您可以尝试使用嵌套的each循环来获取内部对象:

$.getJSON('data.json', function (data) {
    $.each(data.questions, function (index, question) {
       $.each(question.qset, function (index1, qset) {
        console.log(qset);
      });
   });
});