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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19618174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:50:09  来源:igfitidea点击:

How to convert complex Java object into JSON

javajsongson

提问by Still Learning

I'm trying to convert a complex Java object into JSON. But I'm not able to do so. I am using Java generics.

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

This the generic object to store page output. I am not posting JsonTest Object structure. it is same as any normal java class with property and setter and getter.

这是用于存储页面输出的通用对象。我没有发布 JsonTest 对象结构。它与任何具有属性、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));

When I run this program with this values, I got the correct output:

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

 [{"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}]

But when I write the following code to achieve generic feature of Java, I do not get the desired output:

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

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

Output is:

输出是:

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

What's wrong with this code? How can I get the desired output? I want to work with generic feature since I want to write code once for Grid and then pass any object to display in grid.

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

Json Test case:

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

}

采纳答案by Maxim Shoustin

Add TypeToken:

添加TypeToken

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

Use Tokenif you want to serialize/de the Collection

使用Token,如果你想序列化/去了Collection

Fixed

固定的

 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));
}

Output:

输出:

{"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}]}