如何从服务器端return正确JSON到DataTablesjava?
How to return the correct JSON from the server-side java to DataTables?
如何连接两个数组,然后 return 结果作为 JSON 到 DataTables?
我正在转换工作数据表以包括服务器端处理,并且卡在 return 从 java 服务器端获取正确的数组。当前数组 returned 是:
List<YthMmbrSectDtls> ymList;
示例数据为:
[{"youthMemberID":"MTQ5","surname":"Tendon","firstName":"Achilles"}]
对于 DataTables 服务器端,我需要在 returned JSON 的开头包含额外的信息,例如:
{"draw":9, "recordsTotal:57", "recordsFiltered:57"[...]}
所以我return:
{"draw":9, "recordsTotal:57", "recordsFiltered:57","data":[{"youthMemberID":"MTQ5","surname":"Tendon","firstName":"Achilles"}]}
至少这是我阅读手册和观看视频的理解。
我当前的代码是:
List<YthMmbrSectDtls> ymList;
String[] dtInfo = {"draw", "recordsTotal", "recordsFiltered"};
ymList = MySQLConnection.getYouthMmbrAllDtls(archived);
//Test to be replaced by database call, as per above
dtInfo[0] = "9";
dtInfo[1] = "57";
dtInfo[2] = "57";
if (ymList == null || ymList.isEmpty()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No members.");
} else {
System.out.println("ymList: " + ymList);
String json = new Gson().toJson(ymList);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
有多种不同的方法可以解决这个问题:
修改 JsonElement 树
您可以先使用 Gson.toJsonTree(Object)
将 ymList
转换为 in-memory JsonArray
,然后将其包装在 JsonObject
中,并在其中添加 dtInfo
属性:
JsonObject dtInfoJsonObj = new JsonObject();
dtInfoJsonObj.addProperty("draw", dtInfo[0]);
dtInfoJsonObj.addProperty("recordsTotal", dtInfo[1]);
dtInfoJsonObj.addProperty("recordsFiltered", dtInfo[2]);
Gson gson = new Gson();
JsonArray ymJsonArray = gson.toJsonTree(ymList).getAsJsonArray();
dtInfoJsonObj.add("data", ymJsonArray);
String json = gson.toJson(dtInfoJsonObj);
将数据包装在单独的 class
或者您可以创建一个单独的 class,例如DtInfo
,其中包含属性和数据,然后让 Gson 对其进行序列化。这种方法可能更有效,因为没有创建 JsonElement 树的中间步骤。
class DtInfo {
// Gson will use the field names during serialization; you can also customize them
// using `@SerializedName`
// Additionally you can make the fields private if you want
public final int draw;
public final int recordsTotal;
public final int recordsFiltered;
public final List<YthMmbrSectDtls> data;
public DtInfo(int draw, int recordsTotal, int recordsFiltered, List<YthMmbrSectDtls> data) {
this.draw = draw;
this.recordsTotal = recordsTotal;
this.recordsFiltered = recordsFiltered;
this.data = data;
}
}
然后像这样创建 JSON:
DtInfo dtInfoObj = new DtInfo(dtInfo[0], dtInfo[1], dtInfo[2], ymList);
String json = new Gson().toJson(dtInfoObj);
另请注意,您可以通过将 Gson
实例存储在 static final
字段中来提高性能。 Gson 是 thread-safe(参见 class documentation),这样它将缓存它在内部用于将对象转换为 JSON.
的类型适配器
另外 Gson 提供 toJson
接受 Appendable
的重载。因此,您可以将 response.getWriter()
传递给这些 Gson 方法,从而避免创建 JSON.
的中间 String
表示
如何连接两个数组,然后 return 结果作为 JSON 到 DataTables?
我正在转换工作数据表以包括服务器端处理,并且卡在 return 从 java 服务器端获取正确的数组。当前数组 returned 是:
List<YthMmbrSectDtls> ymList;
示例数据为:
[{"youthMemberID":"MTQ5","surname":"Tendon","firstName":"Achilles"}]
对于 DataTables 服务器端,我需要在 returned JSON 的开头包含额外的信息,例如:
{"draw":9, "recordsTotal:57", "recordsFiltered:57"[...]}
所以我return:
{"draw":9, "recordsTotal:57", "recordsFiltered:57","data":[{"youthMemberID":"MTQ5","surname":"Tendon","firstName":"Achilles"}]}
至少这是我阅读手册和观看视频的理解。
我当前的代码是:
List<YthMmbrSectDtls> ymList;
String[] dtInfo = {"draw", "recordsTotal", "recordsFiltered"};
ymList = MySQLConnection.getYouthMmbrAllDtls(archived);
//Test to be replaced by database call, as per above
dtInfo[0] = "9";
dtInfo[1] = "57";
dtInfo[2] = "57";
if (ymList == null || ymList.isEmpty()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No members.");
} else {
System.out.println("ymList: " + ymList);
String json = new Gson().toJson(ymList);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}
有多种不同的方法可以解决这个问题:
修改 JsonElement 树
您可以先使用 Gson.toJsonTree(Object)
将 ymList
转换为 in-memory JsonArray
,然后将其包装在 JsonObject
中,并在其中添加 dtInfo
属性:
JsonObject dtInfoJsonObj = new JsonObject();
dtInfoJsonObj.addProperty("draw", dtInfo[0]);
dtInfoJsonObj.addProperty("recordsTotal", dtInfo[1]);
dtInfoJsonObj.addProperty("recordsFiltered", dtInfo[2]);
Gson gson = new Gson();
JsonArray ymJsonArray = gson.toJsonTree(ymList).getAsJsonArray();
dtInfoJsonObj.add("data", ymJsonArray);
String json = gson.toJson(dtInfoJsonObj);
将数据包装在单独的 class
或者您可以创建一个单独的 class,例如DtInfo
,其中包含属性和数据,然后让 Gson 对其进行序列化。这种方法可能更有效,因为没有创建 JsonElement 树的中间步骤。
class DtInfo {
// Gson will use the field names during serialization; you can also customize them
// using `@SerializedName`
// Additionally you can make the fields private if you want
public final int draw;
public final int recordsTotal;
public final int recordsFiltered;
public final List<YthMmbrSectDtls> data;
public DtInfo(int draw, int recordsTotal, int recordsFiltered, List<YthMmbrSectDtls> data) {
this.draw = draw;
this.recordsTotal = recordsTotal;
this.recordsFiltered = recordsFiltered;
this.data = data;
}
}
然后像这样创建 JSON:
DtInfo dtInfoObj = new DtInfo(dtInfo[0], dtInfo[1], dtInfo[2], ymList);
String json = new Gson().toJson(dtInfoObj);
另请注意,您可以通过将 Gson
实例存储在 static final
字段中来提高性能。 Gson 是 thread-safe(参见 class documentation),这样它将缓存它在内部用于将对象转换为 JSON.
另外 Gson 提供 toJson
接受 Appendable
的重载。因此,您可以将 response.getWriter()
传递给这些 Gson 方法,从而避免创建 JSON.
String
表示