如何将JSONArray从一个JSP传递到另一个JSP并在表中显示结果?

时间:2022-04-25 11:52:12

Below is my JSP page (abc.jsp) from which I am calling another JSP page jspConnection.jsp using the jQuery and then jspConnection.jsp will return me back the query result to abc.jsp and then I need to use the result to show them in a table -

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

Below code I have in abc.jsp from which I am calling jspConnection.jsp -

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

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

Table should look like this in abc.jsp after iterating the JSONObject -

在迭代JSONObject之后,表应该在abc.jsp中看起来像这样 -

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

Now below is the jspConnection.jsp from which I am returning the result of my sql query to abc.jsp page. My SQL query will return me multiple rows.

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

Below is my sql query which I am executing -

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

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

Now I need to return a JSON Object of my above SELECT query -

现在我需要返回上面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());

Now I am not sure how to return the JSONObject such that I can make the table in abc.jsp correctly.. As currently the way I am making JSONObject and JSONArray is not right I guess so not able to understand how to do it correctly?

现在我不知道如何返回JSONObject,这样我就可以正确地在abc.jsp中创建表了。目前我正在制作JSONObject和JSONArray的方式不正确我想是不能理解如何正确地做到这一点?

1 个解决方案

#1


0  

You need to create a new JSONObject at the beginning of each iteration, and you must add it to your JSONArray at the end of each iteration. Perhaps like this,

您需要在每次迭代开始时创建一个新的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.
}

#1


0  

You need to create a new JSONObject at the beginning of each iteration, and you must add it to your JSONArray at the end of each iteration. Perhaps like this,

您需要在每次迭代开始时创建一个新的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.
}