1

我有一个非常复杂的 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

任何帮助将不胜感激!(:

4

1 回答 1

0
  1. 我不明白打包成 JSON 的原因,如果您只想使用 scriptlet 并显示它。您可以简单地将output对象本身通过上下文发送到重定向页面。

  2. 我猜你有别的想法。谈到这个问题,你可以这样做:

    String temp=map = (LinkedHashMap) context.getAttribute("message");
    JsonParser jp = new JsonParser();
    JsonElement ele = jp.parse(temp);
    JsonObject obj=ele.getAsJsonObject();
    JsonArray jArr =Jsobj.get("加载的记录数").getAsJsonArray();

更新

迭代 JsonArray 的方式不正确,这里有一个关于如何迭代 JsonArray 的示例:

                Iterator<JsonElement> msg = jsonArray.iterator();
                while (msg.hasNext()) {
                    ////prints json array values
                    JsonElement eleTemp = msg.next();
                    JsonObject jObj = eleTemp.getAsJsonObject();
                    //... Other code
                }
于 2013-11-06T20:11:00.357 回答