Java Json数据如何使用Jsp传递到DataTable
我正在使用jsp创建简单的crud系统。通过json格式传递的数据我通过控制台测试过,它传递得很好,但数据没有显示在datatable上。我不知道为什么。我写了迄今为止我尝试过的东西 表格Java Json数据如何使用Jsp传递到DataTable,java,jsp,datatables,Java,Jsp,Datatables,我正在使用jsp创建简单的crud系统。通过json格式传递的数据我通过控制台测试过,它传递得很好,但数据没有显示在datatable上。我不知道为什么。我写了迄今为止我尝试过的东西 表格 <table id="tbl-projects" class="table table-responsive table-bordered" cellspacing="0" width="100%"> <thead> <tr>
<table id="tbl-projects" class="table table-responsive table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
</table>
<%@page import="org.json.simple.JSONObject"%>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
Connection con;
PreparedStatement pst;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/studcrud", "root","");
String query="select * from records";
Statement stmt=con.createStatement();
rs=stmt.executeQuery(query);
while(rs.next())
{
String id =rs.getString("id");
String name =rs.getString("name");
String course =rs.getString("course");
String fee =rs.getString("fee");
JSONObject json = new JSONObject();
json.put("name", name);
json.put("course", course);
json.put("fee", fee);
json.put("id", id);
out.print(json);
out.flush();
}
%>
我已附上下面的Datatable图像
JSON格式的响应无效(对象之间缺少comas) 使用
org.json.simple.JSONArray
创建对象列表,例如:
<%@page import="org.json.simple.JSONArray"%>
...
JSONArray list = new JSONArray();
while(rs.next())
{
String id = rs.getString("id");
String name = rs.getString("name");
String course = rs.getString("course");
String fee = rs.getString("fee");
JSONObject obj = new JSONObject();
obj.put("name", name);
obj.put("course", course);
obj.put("fee", fee);
obj.put("id", id);
list.put(obj);
}
out.print(list.toJSONString());
out.flush();
...
JSONArray list=新的JSONArray();
while(rs.next())
{
字符串id=rs.getString(“id”);
字符串名称=rs.getString(“名称”);
字符串课程=rs.getString(“课程”);
字符串费=rs.getString(“费用”);
JSONObject obj=新的JSONObject();
obj.put(“名称”,名称);
obj.put(“航向”,航向);
实物期权(“费用”,费用);
对象放置(“id”,id);
列表。放置(obj);
}
out.print(list.toJSONString());
out.flush();
sir list.put(obj);这里我得到了找不到符号的错误,这个lne也得到了错误。print(list.toJSONString());只有一个问题,先生。表加载成功。数据为循环。仅显示一行记录。
{"fee":"10000","name":"john","course":"java","id":"1"}{"fee":"7000","name":"Raja","course":"C#","id":"2"}{"fee":"2323","name":"sad","course":"asd","id":"3"}{"fee":"12000","name":"Nishan","course":"Jsp","id":"4"}
<%@page import="org.json.simple.JSONArray"%>
...
JSONArray list = new JSONArray();
while(rs.next())
{
String id = rs.getString("id");
String name = rs.getString("name");
String course = rs.getString("course");
String fee = rs.getString("fee");
JSONObject obj = new JSONObject();
obj.put("name", name);
obj.put("course", course);
obj.put("fee", fee);
obj.put("id", id);
list.put(obj);
}
out.print(list.toJSONString());
out.flush();