【发布时间】: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