如何将复杂的Java对象转换为JSON

时间:2013-10-27 12:45:18

标签: java json gson

我正在尝试将复杂的Java对象转换为JSON。但我无法这样做。 我正在使用Java泛型。

这是存储页面输出的通用对象。我没有发布JsonTest对象结构。它与任何带有property和setter和getter的普通java类相同。

class ListRow<T>{

    private int total;
    private int currentRow;
    private List<T> test;


public ListRow(int total, int currentRow, List<T> test) {
    this.total = total;
    this.currentRow = currentRow;
    this.test = test;
}
public int getTotal() {
    return total;
}
public void setTotal(int total) {
    this.total = total;
}
public int getCurrentRow() {
    return currentRow;
}
public void setCurrentRow(int currentRow) {
    this.currentRow = currentRow;
}
public List<T> getTest() {
    return test;
}
public void setTest(List<T> test) {
    this.test = test;
}



    JsonTest test = new JsonTest("naveen", 20, 20000);
JsonTest test2 = new JsonTest("parveen", 20, 20000);
JsonTest test3 = new JsonTest("pawan", 20, 20000);
JsonTest test4 = new JsonTest("anil", 20, 20000);

List<JsonTest> list = new ArrayList<JsonTest>();
list.add(test);
list.add(test2);
list.add(test3);
list.add(test4);

Gson gson = new Gson();
System.out.print(gson.toJson(list));

当我使用此值运行此程序时,我得到了正确的输出:

 [{"name":"naveen","age":20,"salary":20000.0},{"name":"parveen","age":20,"salary":20000.0},{"name":"pawan","age":20,"salary":20000.0},{"name":"anil","age":20,"salary":20000.0}]

但是当我编写以下代码来实现Java的通用功能时,我没有得到所需的输出:

Gson gson = new Gson();
ListRow<JsonTest> dataList = new ListRow<JsonTest>(2, 2, list);
System.out.print(gson.toJson(dataList));

输出是:

{"total":2,"currentRow":2,"test":[{},{},{},{}]}. 

这段代码有什么问题?如何获得所需的输出? 我想使用通用功能,因为我想为Grid编写一次代码,然后传递任何对象以在网格中显示。

Json测试案例:

public static void main(String[] args) {

    JsonTest test = new JsonTest("naveen", 20, 20000);
    JsonTest test2 = new JsonTest("parveen", 20, 20000);
    JsonTest test3 = new JsonTest("pawan", 20, 20000);
    JsonTest test4 = new JsonTest("anil", 20, 20000);

    List<JsonTest> list = new ArrayList<JsonTest>();
    list.add(test);
    list.add(test2);
    list.add(test3);
    list.add(test4);

    Gson gson = new Gson();
    System.out.print(gson.toJson(list));

    ListRow<JsonTest> dataList = new ListRow<JsonTest>(2, 2, list);
    System.out.print(gson.toJson(dataList));

}

1 个答案:

答案 0 :(得分:5)

添加TypeToken

Type type = new TypeToken<ListRow<JsonTest>>() {}.getType();
System.out.println(gson.toJson(dataList, type));

如果要序列化/取消Token

,请使用Collection

已修复

 public static void main(String[] args) {
    Gson gson = new Gson();

    JsonTest test = new JsonTest("naveen", 20, 20000);
    JsonTest test2 = new JsonTest("parveen", 20, 20000);
    JsonTest test3 = new JsonTest("pawan", 20, 20000);
    JsonTest test4 = new JsonTest("anil", 20, 20000);

    List<JsonTest> list = new ArrayList<JsonTest>();
    list.add(test);
    list.add(test2);
    list.add(test3);
    list.add(test4);

    ListRow<JsonTest> dataList = new ListRow<JsonTest>(2, 2, list);

    Type type = new TypeToken<ListRow<JsonTest>>() {}.getType();

    System.out.println(gson.toJson(dataList, type));
}

输出:

{"total":2,"currentRow":2,"test":[{"name":"naveen","age":20,"salary":20000.0},{"name":"parveen","age":20,"salary":20000.0},{"name":"pawan","age":20,"salary":20000.0},{"name":"anil","age":20,"salary":20000.0}]}