You need Json to convert the table format

I am getting the results in json format, I need to display the results in tabular format, getting input from html, executing its servlet program, sparql query shows the result in Json format, can anyone help me show the result in tabular format?

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, a set of code is executed, and the results are displayed in json format, so someone can help me to get the results in table format.

+2


a source to share


1 answer


Your actual problem is that you don't know how to handle the JSON string on the client side. I highly recommend using jQuery as it makes DOM manipulation easier. I suggest going through their tutorials or going to a decent book on the topic.

For jQuery + JSON + Servlet + HTML table mix, I've posted similar answers before here and here with code examples on how to populate a table using Google Gson and a servlet, you may find it helpful. I will copy from one of them.

Here is the servlet and javab:

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

      

JsonServlet

(you can call it whatever you want, this is just a basic example) must be matched in web.xml

the known url-pattern

, so use /json

in this example. The class Data

only represents one row of your HTML table (and your database table).



Now, how can you load the table using 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));
    });
});

      

tableid

of course denotes id

the HTML element in question <table>

.

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

      

It should be like this. At the end of the day, it's pretty easy, trust me. Good luck.

+9


a source







All Articles