2015-12-13 245 views
2

我有JSON作爲JSON解析失敗

{ 
    "js":[ 
     [ 
     "556", 
     "Name the Bengali film television and stage actor who passed away recently?", 
     "Gaynar Baksha", 
     "Byomkesh Bakshi", 
     "Pijush Ganguly", 
     "Kaushik Ganguly", 
     "Pijush Ganguly", 
     "Beginner", 
     "Android", 
     "First Mobile OS by google" 
     ], 
     [ 
     "557", 
     "Venkateswara Rao passed away recently. He was a", 
     "Telugu poet", 
     "Telugu director", 
     "Telugu actor", 
     "Telugu lyricist", 
     "Telugu actor", 
     "Beginner", 
     "Android", 
     "First Mobile OS by google" 
     ], 
     [ 
     "559", 
     "Which Indian cricketer who became the fourth player to register hundred in ODIs? ", 
     "Ishant Sharma", 
     "Virendra Sehwag", 
     "MS Dhoni", 
     "Virat Kohli", 
     "Virat Kohli", 
     "Beginner", 
     "Android", 
     "First Mobile OS by google" 
     ], 
     [ 
     "568", 
     "First Android Mobile ?", 
     "Motorola", 
     "Nokia", 
     "Lenevo", 
     "HTC", 
     "Motorola", 
     "Moderate", 
     "Android", 
     "Moto is the first company" 
     ], 
     [ 
     "569", 
     "col1", 
     "col2", 
     "col3", 
     "col4", 
     "", 
     "", 
     "", 
     "", 
     "" 
     ], 
     [ 
     "570", 
     "\u092d\u093e\u0930\u0924 \u0915\u0947 \u092a\u093e\u0938 125 \u092a\u0930\u092e\u093e\u0923\u0941 \u0939\u0925\u093f\u092f\u093e\u0930, \u091c\u093e\u0928\u093f\u090f; \u0915\u093f\u0938\u0915\u0940 \u0915\u093f\u0924\u0928\u0940 \u0924\u093e\u0915\u0924", 
     "\u092a\u093e\u0938 125 \u092a\u0930\u092e\u093e\u0923\u0941", 
     "data", 
     "Php", 
     "", 
     "", 
     "", 
     "", 
     "" 
     ], 
     [ 
     "571", 
     "\u092a\u093e\u0938 125 \u092a\u0930\u092e\u093e\u0923\u0941", 
     "\u0915\u093f\u0938\u0915\u0940 \u0915\u093f\u0924\u0928\u0940 \u0924\u093e\u0915\u0924", 
     "video", 
     "excel with php", 
     "", 
     "", 
     "", 
     "", 
     "" 
     ] 
    ] 
} 

當我檢查它http://www.jsoneditoronline.org/它顯示正確,即驗證,但是當我在HTML中使用AJAX和JQuery是未能追加其表附加。

我的代碼,將其追加爲:

$.ajax({ 
    type: "GET", 
    url: "searchall.php", 
    contentType: "application/json; charset=utf-8", 
    success: function(data) { 
     var response = $.parseJSON(data); 

     for (i = 0; i < response.length; i++) { 
      $('.dataTableReport').append('<tr style="text-align:center;"><td style="width:6.8%"><span><input name="qid" type="checkbox" value="' + response[i].Question_id + '" style="margin-left: 5%;"/>&nbsp&nbsp&nbsp<label for="qid">' + response[i].Question_id + '</label></span></td><td style="width:20.77%">' + response[i].Question + '</td><td style="width:9.3%">' + response[i].opt1 + '</td><td style="width:9%">' + response[i].opt2 + '</td><td style="width:9%">' + response[i].opt3 + '</td><td style="width:9.8%">' + response[i].opt4 + '</td><td style="width:9.1%">' + response[i].correct + '</td><td style="width:17.3%">' + response[i].explain + '</td><td style="width:5%" onclick="edit(' + response[i].Question_id + ')"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></td><td style="width:5%" onclick="deletee(' + response[i].Question_id + ')"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span></td></tr>'); 
     } 
    } 
}); 

所有表中的字段都充滿了「不確定」

如何解析JSON,以及如何追加?

我已經修改我的JSON作爲

[ { "Question_id":"556", "Question":"Name the Bengali film television and stage actor who passed away recently?", "opt1":"Gaynar Baksha", "opt2":"Byomkesh Bakshi", "opt3":"Pijush Ganguly", "opt4":"Kaushik Ganguly", "correct":"Pijush Ganguly", "Level":"Beginner", "Category":"Android", "explain":"First Mobile OS by google" }, { "Question_id":"557", "Question":"Venkateswara Rao passed away recently. He was a", "opt1":"Telugu poet", "opt2":"Telugu director", "opt3":"Telugu actor", "opt4":"Telugu lyricist", "correct":"Telugu actor", "Level":"Beginner", "Category":"Android", "explain":"First Mobile OS by google" }, { "Question_id":"559", "Question":"Which Indian cricketer who became the fourth player to register hundred in ODIs? ", "opt1":"Ishant Sharma", "opt2":"Virendra Sehwag", "opt3":"MS Dhoni", "opt4":"Virat Kohli", "correct":"Virat Kohli", "Level":"Beginner", "Category":"Android", "explain":"First Mobile OS by google" }, { "Question_id":"568", "Question":"First Android Mobile ?", "opt1":"Motorola", "opt2":"Nokia", "opt3":"Lenevo", "opt4":"HTC", "correct":"Motorola", "Level":"Moderate", "Category":"Android", "explain":"Moto is the first company" }, { "Question_id":"569", "Question":"col1", "opt1":"col2", "opt2":"col3", "opt3":"col4", "opt4":"", "correct":"", "Level":"", "Category":"", "explain":"" }, { "Question_id":"570", "Question":"\u092d\u093e\u0930\u0924 \u0915\u0947 \u092a\u093e\u0938 125 \u092a\u0930\u092e\u093e\u0923\u0941 \u0939\u0925\u093f\u092f\u093e\u0930, \u091c\u093e\u0928\u093f\u090f; \u0915\u093f\u0938\u0915\u0940 \u0915\u093f\u0924\u0928\u0940 \u0924\u093e\u0915\u0924", "opt1":"\u092a\u093e\u0938 125 \u092a\u0930\u092e\u093e\u0923\u0941", "opt2":"data", "opt3":"Php", "opt4":"", "correct":"", "Level":"", "Category":"", "explain":"" }, { "Question_id":"571", "Question":"\u092a\u093e\u0938 125 \u092a\u0930\u092e\u093e\u0923\u0941", "opt1":"\u0915\u093f\u0938\u0915\u0940 \u0915\u093f\u0924\u0928\u0940 \u0924\u093e\u0915\u0924", "opt2":"video", "opt3":"excel with php", "opt4":"", "correct":"", "Level":"", "Category":"", "explain":"" } ]

,仍然下面的代碼是不工作

$.ajax({ 
       type: "GET", 
       url: "sall.php" , 
       dataType: "text", 
       success : function(data) 
       { 
       var response = $.parseJSON(data);  
for (i = 0; i < response.length; i++) 
       { 
$('.dataTableReport').append('<tr style="text-align:center;"><td style="width:6.8%"><span><input name="qid" type="checkbox" value="' + response[i].Question_id + '" style="margin-left: 5%;"/>&nbsp&nbsp&nbsp<label for="qid">' + response[i].Question_id + '</label></span></td><td style="width:20.77%">' + response[i].Question + '</td><td style="width:9.3%">' + response[i].opt1 + '</td><td style="width:9%">' + response[i].opt2 + '</td><td style="width:9%">' + response[i].opt3 + '</td><td style="width:9.8%">' + response[i].opt4 + '</td><td style="width:9.1%">' + response[i].correct + '</td><td style="width:17.3%">' + response[i].explain + '</td><td style="width:5%" onclick="edit('+response[i].Question_id+')"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></td><td style="width:5%" onclick="deletee('+response[i].Question_id+')"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span></td></tr>'); 
       }   
     } 
     }); 

我正在從服務器的響應,但在行var response = $.parseJSON(data);執行得到終止。我很困惑爲什麼parseJSON(數據);不管用?

+0

你確定你的服務器正在發送響應嗎?您可以使用瀏覽器的開發人員控制檯檢查從服務器獲得的響應嗎? – Mike

+0

您在「js」屬性中的值只是值列表,您將無法通過使用諸如「Question_id」之類的鍵訪問它們,只能通過整數索引來訪問它們 –

+0

是的,我得到的響應 –

回答

0

更新線

for (i = 0; i < response.length; i++) 

for (i = 0; i < response['js'].length; i++) 

和HTML追加流內更換

response[i] -> response['js'][i] 

另一種選擇是,以取代線:

response = $.parseJSON(data); 

response = $.parseJSON(data)['js']; 
+0

它不工作 –

+0

可能它不工作,因爲你試圖訪問不存在的項目的屬性,所以你需要修改字符串您將追加使用現有的屬性名稱。 –

2

如果你改變你的PHP代碼,使其返回JSON的代替文本,那麼你不」 t需要解析返回的數據(var response = $.parseJSON(data);)。然後下面的代碼可能會在這種情況下工作:

$.ajax({ 
     type: "GET", 
     url: "sall.php" , 
     dataType: "json", 
     success : function(data) { 
      // this works with the original js data 
      $.each(data.js, function(index, item) { 
       // the two lines below are for testing, you can replace 
       // those with your $('.dataTableReport').append code 
       console.debug(item[0]) 
       console.debug(item[1]) 
      });   
     } 
    }); 

在上面的代碼中dataType改爲json,並使用jQuery的$.each方法。我做了一個JSFiddle,它顯示了$.each如何循環你的json項目。這裏是類似類型的Stackoverflow問題的鏈接:Iterating through JSON within a $.ajax success