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


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

最佳答案

我不理解打包为JSON的原因,如果您要做的就是使用scriptlet并显示它。您只需将output对象本身通过上下文发送到重定向页面即可。
我想您还有其他想法。谈论这个问题,这里是你怎么做的:


    String temp=map = (LinkedHashMap) context.getAttribute("message");
    JsonParser jp = new JsonParser();
    JsonElement ele = jp.parse(temp);
    JsonObject obj=ele.getAsJsonObject();
    JsonArray jArr =Jsobj.get("number of record loaded").getAsJsonArray();

Update

The way of iterating over the JsonArray isn't correct, here's a sample on how to iterate over JsonArray :

                Iterator<JsonElement> msg = jsonArray.iterator();
                while (msg.hasNext()) {
                    ////prints json array values
                    JsonElement eleTemp = msg.next();
                    JsonObject jObj = eleTemp.getAsJsonObject();
                    //... Other code
                }

关于java - 将json字符串打印到html表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19808462/

10-08 22:35