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

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

问题描述:

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

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 -

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

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

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

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

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

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

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

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.

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

Below is my sql query which I am executing -

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

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

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

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的方法不正确,我想因此无法理解该怎么做.正确吗?

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,并且必须在每次迭代的末尾将其添加到您的JSONArray中.也许是这样,

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,

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.
}