【问题标题】:How to pass the JSONArray from one JSP to another JSP and show the results in table?如何将 JSONArray 从一个 JSP 传递到另一个 JSP 并在表格中显示结果?
【发布时间】:2014-01-12 00:53:20
【问题描述】:

下面是我的 JSP 页面 (abc.jsp),我从中使用 jQuery 调用另一个 JSP 页面 jspConnection.jsp,然后 jspConnection.jsp 会将查询结果返回给 abc.jsp,然后我需要使用结果来在表格中显示它们 -

下面是我在 abc.jsp 中调用 jspConnection.jsp 的代码 -

$.post("jspConnection.jsp", {'id': id},
        function (data) {
            // make a new table here from JSONObject
            // and show the result here
        }
);

迭代 JSONObject 后,abc.jsp 中的表应该如下所示 -

FirstName           LastName                Address             Email                   PhoneNumber
SomeValue           SomeOtherValue          SomeOtherValue      SomeOtherValue          SomeOtherValue
...                 ...                     ...                 ....                    ....
...                 ...                     ...                 ....                    ....

现在下面是jspConnection.jsp,我从中将我的sql查询结果返回到abc.jsp页面。我的 SQL 查询将返回多行。

下面是我正在执行的 sql 查询 -

SELECT FirstName, LastName, Libs, Email, PhoneNumber from Testing;

现在我需要返回上述 SELECT 查询的 JSON 对象 -

JSONObject obj = new JSONObject();
JSONArray list = new JSONArray();

while (resultSet.next()) {

// This might be wrong way to make an object for my scenario
    obj.put("FirstName", rs.getString(1)) ;
    obj.put("LastName", rs.getString(2)) ;
    obj.put("Address", rs.getString(3)) ;
    obj.put("Email", rs.getString(4)) ;
    obj.put("PhoneNumber", rs.getString(5)) ;
}
list.add(obj);

response.getWriter().write(obj.toString());

现在我不知道如何返回 JSONObject 以便我可以正确地在 abc.jsp 中制作表格。目前我制作 JSONObject 和 JSONArray 的方式不正确,我想我无法理解该怎么做正确吗?

【问题讨论】:

    标签: java json jsp arrays jsonobject


    【解决方案1】:

    您需要在每次迭代开始时创建一个新的JSONObject,并且必须在每次迭代结束时将其添加到您的JSONArray。或许是这样,

    JSONArray list = new JSONArray();
    
    while (resultSet.next()) {
      JSONObject obj = new JSONObject();       // Move this here.
      // This might be wrong way to make an object for my scenario
      obj.put("FirstName", rs.getString(1));
      obj.put("LastName", rs.getString(2));
      obj.put("Address", rs.getString(3));
      obj.put("Email", rs.getString(4));
      obj.put("PhoneNumber", rs.getString(5));
      list.add(obj);                           // And this here.
    }
    

    【讨论】:

    • 感谢 Elliott 的建议。知道如何将此 JSONArray 发送到我的abc.jsp,然后相应地生成表吗?
    • 你可能不应该那样做。将 JSONArray 返回给客户端,并在浏览器中使用 jQuery 渲染。
    • 您在这里说的是哪个客户?在我的场景中,客户端是 abc.jsp,它正在调用 jspConnection.jsp,这个 jspConnection.jsp 运行选择查询并将数据返回给 abc.jsp,以便结果可以显示在 abc.jsp
    • @akiiddweeber 那你为什么要将数据序列化为 JSON?只需将 HTML 编写为“jspConnection.jsp”,以便在“abc.jsp”上呈现它
    • 我正在序列化数据,因为我认为这是将对象从一个 JSP (jspConnection.jsp) 发送到另一个 JSP(abc.jsp) 的唯一方法。简而言之,我需要在abc.jsp 页面中生成一个包含所有列及其值的表。这就是我调用 jspConnection.jsp 的原因,它可以为我提供 JSONObject,我可以使用它来反序列化并在表..我不能按原样写,因为我在abc.jsp 中还有另一个表,我需要将这个新表呈现在我原来的表下方..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    相关资源
    最近更新 更多