如何ResultSet对象转换为JSON格式输出 [英] How to convert a ResultSet object to a JSON format output
问题描述
我有烧制查询后一个结果。请让我知道我可以将其转换为在JSP中的JSON输出。
I have got a ResultSet after firing a query. Kindly let me know how i can convert it to a JSON output in JSP.
在第二阶段,让我们假设,我们在这个环节得到了JSON输出状> http://inknpost.com/ _ECSHOP演示站/ json.jsp
In the second stage, lets assume that we have got a JSON output like in this link > http://inknpost.com/eshopping/json.jsp
上面的文件通过一个$ .getJSON访问();在另一个文件。
The above file is being accessed by a $.getJSON(); in another file.
请让我知道我怎么能显示姓名,并在页面中的不同行的部门。
Kindly let me know how can i display the "names" and the "departments" in different rows in a page.
推荐答案
创建一个重新presents一行一种可重复使用JavaBean类,一个单一的实体。
Create a reuseable Javabean class which represents one row, a single entity.
public class Category {
private Long id;
private String name;
private String department;
// Add/generate getters/setters/c'tors/equals/hashcode and other boilerplate.
}
创建一个映射的ResultSet
来这些JavaBeans集合了常用的JDBC方式。
Create a reuseable DAO class which maps the ResultSet
to a collection of those Javabeans the usual JDBC way.
public class CategoryDAO {
private static final String SQL_LIST = "SELECT id, name, department FROM category";
// ...
public List<Category> list() throws SQLException {
List<Category> categories = new ArrayList<Category>();
try (
Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement(SQL_LIST);
ResultSet resultSet = statement.executeQuery();
) {
while (resultSet.next()) {
Category category = new Category();
category.setId(resultSet.getLong("id"));
category.setName(resultSet.getString("name"));
category.setDepartment(resultSet.getString("department"));
categories.add(category);
}
}
return categories;
}
// ...
}
创建它使用JSON串行器/解串器,其能够到Java Bean的arbirary收集和JSON字符串之间进行转换,如的谷歌GSON 。
@WebServlet("/categories.json")
public class CategoriesJsonServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<Category> categories = categoryDAO.list();
String categoriesJson = new Gson().toJson(categories);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(categoriesJson);
} catch (SQLException e) {
throw new ServletException("DB error", e);
}
}
}
按 HTTP调用它://本地主机:8080 / CONTEXTNAME / categories.json
。没有,没有JSP参与。你不应该使用JSP为其他输出格式不是HTML。
Invoke it by http://localhost:8080/contextname/categories.json
. No, there is no JSP involved. You should not be using JSP for output formats other than HTML.
最后,在jQuery的,只是访问它通常的 $。的getJSON()
办法。
Finally, in jQuery, just access it the usual $.getJSON()
way.
$('#somebutton').click(function() {
$.getJSON('categories.json', function(categoriesJson) {
var $table = $('<table>').appendTo($('#somediv'));
$.each(categoriesJson, function(index, category) {
$('<tr>').appendTo($table)
.append($('<td>').text(category.id))
.append($('<td>').text(category.name))
.append($('<td>').text(category.department));
});
});
});
这篇关于如何ResultSet对象转换为JSON格式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!