2015-10-15 138 views
1
$.getJSON('data.json', function (data) { 
    $.each(data.questions, function (index, data) { 
     console.log(data); 
    }); 
}); // getJSON 

數據JSON:如何顯示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" 
      } 
     }, 
    ] 
} 
+1

可能的重複[通過JSON使用顯示數據的最佳方法g jQuery](http://stackoverflow.com/questions/327231/best-way-to-display-data-via-json-using-jquery) – madforstrength

+0

也見http://stackoverflow.com/questions/33086322/handlebars - 如何導航 - json的數據模板/ 33094038#33094038 – wazz

+0

也,這是無效的json。 (http://jsonlint.com/)。 – wazz

回答

1

JUST驗證您的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 
    ] 
} 
0

您可以嘗試使用嵌套each環獲得在內部對象:

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