简体   繁体   English

需要Json结果为表格格式

[英]Need Json results in a Table Format

I am getting results in json format, i need to display my results in a table format, Getting a input from html, executing it servlet program, the sparql query shows result in Json format, can anybody help me in showing the result in table format? 我得到json格式的结果,我需要以表格格式显示我的结果,从html获取输入,执行它的servlet程序,sparql查询以Json格式显示结果,任何人都可以帮我以表格格式显示结果?

response.setContentType("json-comment-filtered");
response.setHeader("Cache-Control","nocache");

OutputStream out = response.getOutputStream();
//  ResultSetFormatter.outputAsXML(out, results);
System.out.println("called");
out.flush();
JSONOutput jOut = new JSONOutput();
jOut.format(out, results);

soon the query is executed, the set of code is executed and results are displayed in json format, so can anybody help me out in way that I can get results in table format. 很快执行查询,执行代码集并以json格式显示结果,所以任何人都可以帮助我以表格格式获得结果。

Your actual problem is that you don't know how to process the JSON string in the client side. 您的实际问题是您不知道如何在客户端处理JSON字符串。 I strongly suggest to use jQuery for this since this simplifies DOM manipulation a lot. 我强烈建议使用jQuery ,因为这样可以简化DOM操作。 I suggest to go through their tutorials or go get a decent book on the subject. 我建议你阅读他们的教程,或者去找一关于这个主题的

For the jQuery + JSON + Servlet + HTML table mix, I've posted similar answers before here and here with code examples how to populate a table with help of Google Gson and a Servlet, you may find it useful. 对于jQuery + JSON + Servlet + HTML表混合,我在这里这里发布了类似的答案,代码示例如何在Google Gson和Servlet的帮助下填充表格,您可能会发现它很有用。 I'll copypaste from one of them. 我会从其中一个中进行复制。

Here are the Servlet and the Javabean: 这是Servlet和Javabean:

public class JsonServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Data> list = dataDAO.list();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(new Gson().toJson(list));
    }
}

public class Data {
    private Long id;
    private String name;
    private Integer value;
    // Add/generate getters/setters.
}

The JsonServlet (you may name it whatever you want, this is just a basic example) should be mapped in web.xml on a known url-pattern , let's use /json in this example. JsonServlet (你可以任意命名,这只是一个基本的例子)应该在web.xml JsonServlet射到一个已知的url-pattern ,让我们在这个例子中使用/json The class Data just represents one row of your HTML table (and the database table). Data只表示HTML表(和数据库表)的一行。

Now, here's how you can load a table with help of jQuery.getJSON : 现在,您可以在jQuery.getJSON的帮助下加载表格:

$.getJSON("http://example.com/json", function(list) {
    var table = $('#tableid');
    $.each(list, function(index, data) {
        $('<tr>').appendTo(table)
            .append($('<td>').text(data.id))
            .append($('<td>').text(data.name))
            .append($('<td>').text(data.value));
    });
});

The tableid of course denotes the id of the HTML <table> element in question. tableid当然表示所讨论的HTML <table>元素的id

<table id="tableId"></table>

That should be it. 那应该是它。 After all it's fairly simple, believe me. 毕竟它相当简单,相信我。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM