需要Json结果为表格格式 [英] Need Json results in a Table Format

查看:103
本文介绍了需要Json结果为表格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以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);

即将执行查询,执行代码集并以json格式显示结果,所以

解决方案

您的实际 问题在于你不知道如何在客户端处理JSON字符串。我强烈建议为此使用 jQuery ,因为这可以简化DOM操作。我建议通过他们的教程或去获取

对于jQuery + JSON + Servlet +关于jQuery + JSON + Servlet的更多信息,请参阅http://go.microsoft.com/fwlink/?LinkId=1(页面可能为英文) HTML表格组合,我已经在 here 此处使用代码示例如何填充一张借助 Google Gson 和一个Servlet的表格,您可能会觉得它很有用。

以下是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));
}
}

公共类数据{
私人长ID;
私人字符串名称;
私人整数值;
//添加/生成getters / setters。

JsonServlet (你可以任意命名它,这只是一个基本的例子)应该映射到 web.xml 上的一个已知的 url-pattern ,在这个例子中我们使用 / json 。类 Data 仅代表HTML表(和数据库表的一行)。



现在,您可以在 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 当然表示<$问题中的HTML < table> 元素的c $ c> id

 < table id =tableId>< / table> 

就是这样。毕竟它很简单,相信我。祝你好运。


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?

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.

解决方案

Your actual problem is that you don't know how to process the JSON string in the client side. I strongly suggest to use jQuery for this since this simplifies DOM manipulation a lot. 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. I'll copypaste from one of them.

Here are the Servlet and the 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. The class Data just represents one row of your HTML table (and the database table).

Now, here's how you can load a table with help of 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 idof the HTML <table> element in question.

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

That should be it. After all it's fairly simple, believe me. Good luck.

这篇关于需要Json结果为表格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆