我有一个非常复杂的 JSON 字符串,它是由 GsonBuilder 创建的(在 xxx.java 中)
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
String message = gson.toJson(output);
out.println(message);
它看起来像这样:
{ "status": "error", "number of record loaded": [ { "bid.csv": 24 }, { "course.csv": 12 }, { "course_completed.csv": 0 }, { "prerequisites.csv": 1 }, { "section.csv": 14 }, { "student.csv": 11 } ], "errorList": [ { "file": "student.csv", "line": 9, "message": [ "duplicate userid" ] }, { "file": "student.csv", "line": 10, "message": [ "invalid e-dollar" ] }, { "file": "student.csv", "line": 11, "message": [ "invalid e-dollar" ] }, { "file": "course.csv", "line": 4, "message": [ "invalid exam start" ] }, { "file": "course.csv", "line": 10, "message": [ "invalid exam date" ] }, { "file": "course.csv", "line": 11, "message": [ "invalid exam date" ] }, { "file": "section.csv", "line": 2, "message": [ "invalid course", "duplicate section" ] }, { "file": "section.csv", "line": 4, "message": [ "invalid section" ] }, { "file": "section.csv", "line": 7, "message": [ "invalid day" ] }, { "file": "section.csv", "line": 8, "message": [ "invalid start" ] }, { "file": "section.csv", "line": 14, "message": [ "invalid size" ] }, { "file": "section.csv", "line": 15, "message": [ "invalid size" ] }, { "file": "section.csv", "line": 18, "message": [ "invalid course", "duplicate section" ] }, { "file": "section.csv", "line": 19, "message": [ "invalid course", "duplicate section" ] }, { "file": "section.csv", "line": 20, "message": [ "invalid course", "duplicate section" ] }, { "file": "section.csv", "line": 21, "message": [ "invalid course", "duplicate section" ] }, { "file": "section.csv", "line": 22, "message": [ "invalid course", "duplicate section" ] }, { "file": "prerequisite.csv", "line": 2, "message": [ "invalid course" ] }, { "file": "prerequisite.csv", "line": 3, "message": [ "invalid prerequisite" ] }, { "file": "course_completed.csv", "line": 2, "message": [ "invalid userid" ] }, { "file": "bid.csv", "line": 2, "message": [ "invalid amount" ] }, { "file": "bid.csv", "line": 3, "message": [ "invalid section" ] }, { "file": "bid.csv", "line": 4, "message": [ "invalid section" ] }, { "file": "bid.csv", "line": 5, "message": [ "not own school course" ] }, { "file": "bid.csv", "line": 6, "message": [ "incomplete prerequisites" ] }, { "file": "bid.csv", "line": 13, "message": [ "section limit reached" ] }, { "file": "bid.csv", "line": 14, "message": [ "insufficient e$" ] } ] }
我希望使用此代码传递此值:
context.setAttribute("message", message);
response.sendRedirect("zzz.jsp");
那么在 zzz.jsp 中,我想如何将上述复杂的 json 字符串打印到表中?它应该显示类似
LinkedHashMap map = null;
String status = null;
JSONArray jArr = null;
try {
map = (LinkedHashMap) context.getAttribute("bootstrapStatus");
status = (String) map.get("status");
out.println("<p><font color = 'red'> Bootstrap Status: " + status + "</font></p>");
jArr = (JSONArray) map.get("num-record-loaded");
} catch (NullPointerException npe){
status = "Error: no bootstrap";
out.println("<p>"+status + "</p>");
}
if (map != null) {
%>
<div class="CSSTableGenerator" >
<table align='center'>
<tr>
<td>CSV File</td>
<td>Number of records</td>
<tr></tr>
<%
for (Object obj : jArr) {
JSONObject jsonObj = (JSONObject) obj;
Set keys = jsonObj.keySet();
Iterator iter = keys.iterator();
while (iter.hasNext()) {
String fileName = (String) iter.next();
int successLine = (Integer) jsonObj.get(fileName);
out.println("<tr>");
out.println("<td>" + fileName + "</td>");
out.println("<td>" + successLine + "</td>");
out.println("</tr>");
}
}
%>
</table>
</div>
<br/>
<p><strong>Error Messages</strong></p>
<%
if (status.equals("error")) {
%>
<div class="CSSTableGenerator" >
<table align='center'>
<tr>
<td>CSV File</td>
<td>Line number</td>
<td>Error message</td>
<tr></tr>
<%
ArrayList<BootstrapError> errArr = (ArrayList<BootstrapError>) map.get("error");
for (BootstrapError error : errArr) {
out.println("<tr>");
out.println("<td>" + error.getFile() + "</td>");
out.println("<td>" + error.getLine() + "</td>");
out.println("<td>" + error.getMessage() + "</td>");
out.println("</tr>");
}
}
}
%>
</table>
我知道这方面有一个错误:map = (LinkedHashMap) context.getAttribute("message");
所以我将其更改为:
String s = (String) context.getAttribute("message");
LinkedHashMap map = new LinkedHashMap();
map.put("message",s);
String status = (String) map.get("status"); <<< this appear null
JSONArray jArr = (JSONArray) map.get("num-record-loaded"); <<< this appears Null
任何帮助将不胜感激!(: