2013-02-21 43 views
1

我試圖從數據庫中使用json對象檢索數據。但是當我調用servlet時,jquery將返回SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data此錯誤僅在響應包含更多數據時才顯示。JSON.parse:JSON數據後意外的非空白字符

我的腳本是:

$.ajax({ 
     type: "GET", 
     url: "VComment", 
     data:'comm='+encodeURIComponent(comm)+'&'+'data-id='+encodeURIComponent(dataid)+'&'+'data-alid='+encodeURIComponent(dataalid), 
     dataType: "json", 
     success: function(data, textStatus, jqXHR) 
     { 
      if(data.success) 
      { 
        var newcommhtml = '<div id="c0'+thecid+'" class="cnew clearfix"> <section class="c-author">'; 
        newcommhtml = newcommhtml + '<h3>Anonymous</h3>'; 
        newcommhtml = newcommhtml + '<span class="pubdate">'+month+' '+day+', '+year+'</span> </section>'; 
        newcommhtml = newcommhtml + '<section class="c-content">'; 
        newcommhtml = newcommhtml + '<img src="images/green-avatar.png" alt="avatar" width="80" height="80" class="ava">'; 
        newcommhtml = newcommhtml + '<p>'+nl2br(data.commentInfo.comment)+'</p> </section></div>'; 

        var thelm = "#c0"+thecid; 
        commwrap.append(newcommhtml); 
        $(thelm).hide().fadeIn('slow'); 

        setTimeout(function() { $(thelm).addClass('green'); }, 800); 

        $("#comm").val(""); 
        thecid++; 

        if(errorspan.html() != null) { 
         errorspan.remove(); 
        } 
      } 

      }, 
    error: function(jqXHR, textStatus, errorThrown) 
     { 
     alert("error"+errorThrown); 
     console.log("Something really bad happened " + textStatus); 
     }, 
}); 

並得到響應..

{"success":true,"commentInfo":{"uname":"shyam","comment":"rreter","itemId":0}} 
    {"success":true,"commentInfo":{"uname":"shyam","comment":"dfdsfdd","itemId":0}} 
    {"success":true,"commentInfo":{"uname":"shyam","comment":"xzdfdsfdd","itemId":0}} 
    {"success":true,"commentInfo":{"uname":"shyam","comment":"sdfsd fsdfs","itemId":0}} 
    {"success":true,"commentInfo":{"uname":"shyam","comment":"sdsd","itemId":0}} 
    {"success":true,"commentInfo":{"uname":"shyam","comment":"dd","itemId":0}} 
    {"success":true,"commentInfo":{"uname":"shyam","comment":"dddf","itemId":0}} 

servlet代碼:

 while(rs.next()){ 
      Commenter comment = new Commenter(); 
      comment.setUname(rs.getString("uname").trim()); 
      comment.setComment(rs.getString("comments").trim()); 
      commentObj=gson.toJsonTree(comment); 
      myObj.add("commentInfo", commentObj); 
      out.println(myObj.toString()); 
      } 

請誰能告訴我怎麼解決這個問題?謝謝....

+1

那麼,響應是無效的JSON。 '{...} {...}'無效。 '{...}'和'[{...},{...}]'會。修復你的服務器代碼喲創建有效的JSON。 – 2013-02-21 13:30:12

+0

輸出不是有效的JSON,所以jQuery無法解析它。 – Sirko 2013-02-21 13:30:51

+0

@Felix Kling但我不知道如何從servlet傳入數組。請告訴我如何傳遞數組中的值...我以不同的方式嘗試了這些,但它們都不適合我...... – james 2013-02-21 13:31:23

回答

1

試試這個代碼,我覺得它可能會解決你的問題:

 ArrayList<JSONObject> CommArray=new ArrayList<JSONObject>(); 

     while(rs.next()){ 
      JSONObject Obj = new JSONObject(); 
      Obj.put("uname",rs.getString("uname").trim());  //Adds your uname to Object 
      Obj.put("comment",rs.getString("comments").trim());//Adds your comment to Object 
      CommArray.add(Obj);        //Inserts your Object to ArrayList 
      System.out.println(rs.getString("comments").trim()); 
      }  
     JSONArray arrayObj=JSONArray.fromObject(CommArray);//Converts the Array List to JSONArray 
     commentObj=gson.toJsonTree(arrayObj); //Converts the JSONArray to Jsontree 
     myObj.add("commentInfo", commentObj); //Adds the Tree to JsonObject as commentInfo Array 
     out.println(myObj.toString());  //Prints the result 
     rs.close();                
     stmt.close();                
     stmt = null;                
     conn.close();                
     conn = null;             

    }                
    catch(Exception e){} 

我希望這能解決你的問題。

+0

謝謝你先生...完美的答案...它對我很好.. – james 2013-02-21 18:56:49

+0

不客氣,我很高興能夠提供幫助。 – 2013-02-21 18:58:06

3

你在響應中有幾個簡單的JSON對象。總結他們在一個數組,你會好起來的

[{"success":true,"commentInfo":{"uname":"shyam","comment":"rreter","itemId":0}}, 
{"success":true,"commentInfo":{"uname":"shyam","comment":"dfdsfdd","itemId":0}}, 
{"success":true,"commentInfo":{"uname":"shyam","comment":"xzdfdsfdd","itemId":0}}, 
{"success":true,"commentInfo":{"uname":"shyam","comment":"sdfsd fsdfs","itemId":0}}, 
{"success":true,"commentInfo":{"uname":"shyam","comment":"sdsd","itemId":0}}, 
{"success":true,"commentInfo":{"uname":"shyam","comment":"dd","itemId":0}}, 
{"success":true,"commentInfo":{"uname":"shyam","comment":"dddf","itemId":0}}] 

和變化的功能:在服務器端

success: function(data, textStatus, jqXHR) 
    { 
     for(var i = 0,len=data.length;i<len;i += 1){ 
     if(data[i].success) 
     { 
      //code 
     } 
     } 
    } 

只是改變

out.println("["); 
Boolean first = true 
while(rs.next()){ 
     Commenter comment = new Commenter(); 
     comment.setUname(rs.getString("uname").trim()); 
     comment.setComment(rs.getString("comments").trim()); 
     commentObj=gson.toJsonTree(comment); 
     myObj.add("commentInfo", commentObj); 
     if(!first){ 
      out.print(","); 
     } else { 
      first = false; 
     } 
     out.println(myObj.toString()); 
     } 
out.println("]"); 
+0

*「後面的逗號將被JSON解析器忽略」*我不這麼認爲。 http://jsfiddle.net/pYELD/ – 2013-02-21 14:33:59

+0

@FelixKling以及我確實parseit :)但試圖不同我得到sameresultas你 – 2013-02-21 16:26:23

0

嘗試暫時改變dataType並做出相同的錯誤請求,輸出響應e讓我們看看有什麼不對。

相關問題