JSON formatting error

JSON formatting error

devilpanksdevilpanks Posts: 5Questions: 0Answers: 0
edited May 2012 in DataTables 1.9
I'm new to DataTable. I tried to implement the server-side processing. These are my files..
index.jsp
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">






$(document).ready(function() {

$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "server.jsp",
"sAjaxDataProp": "data",
"sServerMethod": "POST"
} );
} );






engine
browser
platform
version
grade



Loading data from server





[/code]

server.jsp

[code]
/*
* Script: DataTables server-side script for Java Sever Pages
* Copyright: 2010 - Wang Chao
* License: GPL v2 or BSD (3-point)
*/

<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>
<%@page import="org.json.*"%>
<%@page import="java.io.*"%>
<%
// String file = "server.txt";
String[] cols = { "engine", "browser", "platform", "version", "grade" };
String table = "test.ajax";

JSONObject result = new JSONObject();
JSONArray array = new JSONArray();

int amount = 10;
int start = 0;
int echo = 0;
int col = 0;

String engine = "";
String browser = "";
String platform = "";
String version = "";
String grade = "";

String dir = "asc";
String sStart = request.getParameter("iDisplayStart");
String sAmount = request.getParameter("iDisplayLength");
String sEcho = request.getParameter("sEcho");
String sCol = request.getParameter("iSortCol_0");
String sdir = request.getParameter("sSortDir_0");

engine = request.getParameter("sSearch_0");
browser = request.getParameter("sSearch_1");
platform = request.getParameter("sSearch_2");
version = request.getParameter("sSearch_3");
grade = request.getParameter("sSearch_4");

List sArray = new ArrayList();
if (!engine.equals("")) {
String sEngine = " engine like '%" + engine + "%'";
sArray.add(sEngine);
//or combine the above two steps as:
//sArray.add(" engine like '%" + engine + "%'");
//the same as followings
}
if (!browser.equals("")) {
String sBrowser = " browser like '%" + browser + "%'";
sArray.add(sBrowser);
}
if (!platform.equals("")) {
String sPlatform = " platform like '%" + platform + "%'";
sArray.add(sPlatform);
}
if (!version.equals("")) {
String sVersion = " version like '%" + version + "%'";
sArray.add(sVersion);
}
if (!grade.equals("")) {
String sGrade = " grade like '%" + grade + "%'";
sArray.add(sGrade);
}

String individualSearch = "";
if(sArray.size()==1){
individualSearch = sArray.get(0);
}else if(sArray.size()>1){
for(int i=0;i 100)
amount = 10;
}
if (sEcho != null) {
echo = Integer.parseInt(sEcho);
}
if (sCol != null) {
col = Integer.parseInt(sCol);
if (col < 0 || col > 5)
col = 0;
}
if (sdir != null) {
if (!sdir.equals("asc"))
dir = "desc";
}
String colName = cols[col];
int total = 0;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","root");
String sql = "SELECT count(*) FROM "+table;
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
total = rs.getInt("count(*)");
}

int totalAfterFilter = total;
result.put("sEcho",echo);


String searchSQL = "";
String sql1 = "SELECT * FROM "+table;
String searchTerm = request.getParameter("sSearch");
String globeSearch = " where (engine like '%"+searchTerm+"%'"
+ " or browser like '%"+searchTerm+"%'"
+ " or platform like '%"+searchTerm+"%'"
+ " or version like '%"+searchTerm+"%'"
+ " or grade like '%"+searchTerm+"%')";
if(searchTerm!=""&&individualSearch!=""){
searchSQL = globeSearch + " and " + individualSearch;
}
else if(individualSearch!=""){
searchSQL = " where " + individualSearch;
}else if(searchTerm!=""){
searchSQL=globeSearch;
}
sql1 += searchSQL;
sql1 += " order by " + colName + " " + dir;
sql1 += " limit " + start + ", " + amount;

PreparedStatement ps1 = conn.prepareStatement(sql1);
ResultSet rs1 = ps1.executeQuery();
while (rs1.next()) {
JSONArray ja = new JSONArray();
ja.put(rs1.getString("engine"));
ja.put(rs1.getString("browser"));
ja.put(rs1.getString("platform"));
ja.put(rs1.getString("version"));
ja.put(rs1.getString("grade"));
array.put(ja);
}


String sql2 = "SELECT count(*) FROM "+table;
if (searchTerm != "") {
sql2 += searchSQL;
PreparedStatement ps2 = conn.prepareStatement(sql2);
ResultSet rs2 = ps2.executeQuery();
if (rs2.next()) {
totalAfterFilter = rs2.getInt("count(*)");
}
}
// result.put("sEcho", echo);
result.put("iTotalRecords", total);
result.put("iTotalDisplayRecords", totalAfterFilter);
result.put("aaData", array);

response.getWriter().flush();
// response.getWriter().print(result);
System.out.print(result);
out.print(result);



conn.close();
} catch (Exception e) {


}
%>
[/code]

I have validated json object on jsonlint.com already.
Please help me out. I'm stuck here badly!!!! plzzz

Replies

  • devilpanksdevilpanks Posts: 5Questions: 0Answers: 0
    json object
    [code]
    {
    "iTotalDisplayRecords": 59,
    "iTotalRecords": 59,
    "aaData": [
    [
    "Gecko",
    "Firefox 1.0",
    "Win 98+ / OSX.2+",
    "1.7",
    "A"
    ],
    [
    "Gecko",
    "Firefox 1.5",
    "Win 98+ / OSX.2+",
    "1.8",
    "A"
    ],
    [
    "Gecko",
    "Firefox 2.0",
    "Win 98+ / OSX.2+",
    "1.8",
    "A"
    ],
    [
    "Gecko",
    "Firefox 3.0",
    "Win 2k+ / OSX.3+",
    "1.9",
    "A"
    ],
    [
    "Gecko",
    "Camino 1.0",
    "OSX.2+",
    "1.8",
    "A"
    ],
    [
    "Gecko",
    "Camino 1.5",
    "OSX.3+",
    "1.8",
    "A"
    ],
    [
    "Gecko",
    "Netscape 7.2",
    "Win 95+ / Mac OS 8.6-9.2",
    "1.7",
    "A"
    ],
    [
    "Gecko",
    "Netscape Browser 8",
    "Win 98SE+",
    "1.7",
    "A"
    ],
    [
    "Gecko",
    "Netscape Navigator 9",
    "Win 98+ / OSX.2+",
    "1.8",
    "A"
    ],
    [
    "Gecko",
    "Mozilla 1.0",
    "Win 95+ / OSX.1+",
    "1",
    "A"
    ]
    ],
    "sEcho": 1
    }
    [/code]
  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Can you run your table through the DataTables debugger please?

    Allan
  • devilpanksdevilpanks Posts: 5Questions: 0Answers: 0
    The Data Table debugger output:

    debug code: oyonog (view)
  • devilpanksdevilpanks Posts: 5Questions: 0Answers: 0
    allan, in firefox web console, it shows aData is undefined. Is this a reason?
  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin
    Yes - your JSON returned is not valid :-).

    You've got a comment at the top of the return from the server:

    [code]
    /*
    * Script: DataTables server-side script for Java Sever Pages
    * Copyright: 2010 - Wang Chao
    * License: GPL v2 or BSD (3-point)
    */
    [/code]

    comments aren't valid in JSON and thus DataTables correctly gives you the warning. You need to remove the comment from the return.

    Allan
  • devilpanksdevilpanks Posts: 5Questions: 0Answers: 0
    Thanx allan, This is working like a charm now...
This discussion has been closed.