【发布时间】:2012-03-18 15:29:58
【问题描述】:
我收到“DataTables 警告:无法解析来自服务器的 JSON 数据。这是由 JSON 格式错误引起的” 我知道这可能是其他线程的副本,我在代码中使用了 java 和 dataTables。我得到的是 JSON 格式的输出,但在打印时客户端无法识别它。
当我验证 http://jsonlint.com/ 中的 JSON 对象时,它的格式正确。我不确定是什么导致了这个错误。
{"iTotalRecords":5,"sEcho":"1","aaData":[["V2993ASFKH230943","本田","雅阁"],["V2993A39SNF30943","本田","CRV"] ,["V4833A39SNF30943","讴歌","TSX"],["V4833RE9SNF30943","讴歌","TL"],["V9383RE9SNF30943","讴歌","MDX"]],"iTotalDisplayRecords":5}
任何帮助将不胜感激。
谢谢
编辑
try {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("sEcho", sEcho);
jsonResponse.put("iTotalRecords", iTotalRecords);
jsonResponse.put("iTotalDisplayRecords", iTotalDisplayRecords);
while (itr.hasNext()) {
Map rs = (Map) itr.next();
FleetEquipment eqpmt = new FleetEquipment();
eqpmt.setVinNumber((String) rs.get("VIN_ID"));
eqpmt.setMake((String) rs.get("MAKE"));
eqpmt.setModel((String) rs.get("MODEL"));
JSONArray row = new JSONArray();
row.put(eqpmt.getVinNumber()).put(eqpmt.getMake())
.put(eqpmt.getModel());
data.put(row);
}
jsonResponse.put("aaData", data);
response.setContentType("application/json");
response.getWriter().print(jsonResponse.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.setContentType("text/html");
response.getWriter().print(e.getMessage());
}
编辑 1
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<portlet:defineObjects/>
<portlet:resourceURL var="listURL" id="list" escapeXml="false"/>
<script type="text/javascript">
$(document).ready(function () {
$('#listTable').dataTable({
"bServerSide": true,
"sAjaxSource": "<%=listURL.toString()%>",
"bProcessing": true,
"sPaginationType": "full_numbers",
"bJQueryUI": true
});
});
</script></head>
<body>
<table id="listTable">
<thead>
<tr>
<th>VIN ID</th>
<th>MAKE</th>
<th>MODEL</th>
<th>MODEL1</th>
<th>Model2</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
【问题讨论】:
-
你确定,上面提到的 JSON 中没有任何额外的词吗?我的意思是任何错误?
-
是的..我已经在上面提到的链接中验证了。
-
我知道你已经验证过了,但是你在萤火虫中检查过,至于最终的结果是什么。那是服务器的萤火虫输出吗?可能是您正在验证正确的 json,但它不是从最终输出中获取的。那么你有没有从最终输出中取出它,比如 firefug?
-
您能否发布尝试在服务器端解析 JSON 的代码?
-
是的,这就是我在萤火虫中得到的响应。
标签: java jquery json datatables