使用Gson生成符合Flexigrid表格插件要求的Json数据。
Flexigrid是Jquery的一个表格插件,可以显示后台传回的Json格式的数据。
关于Flexigrid:http://flexigrid.info/
Flexigrid对Json格式有特殊要求,其格式为:
{
"total":4, //总记录数
"page":1, //页数
"rows":[ //对象
{"id":0,"cell":{"a":"测试1","b":"纯测试1"}}, //cell里面的才是真正的内容
{"id":1,"cell":{"a":"测试2","b":"纯测试2"}},
{"id":2,"cell":{"a":"测试3","b":"纯测试3"}},
{"id":3,"cell":{"a":"测试3","b":"纯测试3"}}
]}
接下来我们使用Gson来把一个List(从数据库中查询得到的结果一般为List)对象转换成符合Flexigrid要求的JSon。
关于Gson:http://code.google.com/p/google-gson/
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
Gson是一个能把Java对象转换成Json格式的Java库,也能将Json转换成一个Java对象。我们在JavaProject中添加gson-2.0.jar即可。
先编辑一个rows对象的类:Flexigridrows.java
1 public class FlexigridRows <T> { //这里使用了泛型
2 private int id; //每个rows里面的id
3 private T cell; //具体对应的对象
4
5 public int getId() {
6 return id;
7 }
8 public void setId(int id) {
9 this.id = id;
10 }
11 public T getCell() {
12 return cell;
13 }
14 public void setCell(T cell) {
15 this.cell = cell;
16 }
17 public FlexigridRows(int id, T cell) {
18 super();
19 this.id = id;
20 this.cell = cell;
21 }
22 }
然后的Flexigrid的类:FlexigridJson.java
1 public class FlexigridJson <T> {
2 private int total;
3 private int page;
4 private List<FlexigridRows<T>> rows; //rows对象有多个 所以使用list
5 public int getTotal() {
6 return total;
7 }
8 public void setTotal(int total) {
9 this.total = total;
10 }
11 public int getPage() {
12 return page;
13 }
14 public void setPage(int page) {
15 this.page = page;
16 }
17
18 public FlexigridJson(int total, int page, List<FlexigridRows<T>> rows) {
19 super();
20 this.total = total;
21 this.page = page;
22 this.rows=rows;
23 }
24
最后是生产Json的类:GetFlexigridJson.java
1 import com.google.gson.Gson;
2
3 public class GetFlexigridJson {
4
5 public <T> String getJson (int page,List<T> object){
6 String result="";
7 int total= object.size();
8 List<FlexigridRows<T>> frlist=new ArrayList<FlexigridRows<T>>();
9 for (int i = 0; i <total; i++) { //使用for循环为rows中的每个id赋值.
10 FlexigridRows<T> fr=new FlexigridRows<T>(i, object.get(i));
11 frlist.add(fr);
12 }
13 FlexigridJson<T> fj=new FlexigridJson<T>(total, page, frlist);
14 Gson gson=new Gson();
15 result=gson.toJson(fj); //使用Gson生产json
16 // System.out.println(result);
17 return result;
18 }
19 }
这样就可以了,代码里使用了泛型保证了通用性。来测试一下吧。
随便在一个类中调用:
1 Whstock stock1=new Whstock("测试1", "纯测试1"); //Whstock是一个测试类
2 Whstock stock2=new Whstock("测试2", "纯测试2");
3 Whstock stock3=new Whstock("测试3", "纯测试3");
4
5 List<Whstock> list=new ArrayList<Whstock>();
6 list.add(stock1);
7 list.add(stock2);
8 list.add(stock3);
9 list.add(stock3);
10
11 GetFlexigridJson getjson=new GetFlexigridJson();
12 String result=getjson.getJson(1, list);
13 System.out.println(result);
执行后的输出结果:
{"total":4,"page":1,"rows":[{"id":0,"cell":{"a":"测试1","b":"纯测试1"}},{"id":1,"cell":{"a":"测试2","b":"纯测试2"}},{"id":2,"cell":{"a":"测试3","b":"纯测试3"}},{"id":3,"cell":{"a":"测试3","b":"纯测试3"}}]}
符合要求,Done!