3

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

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.

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:

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.

Json Test case:

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

}
3
  • Enter "gson" in your browser search bar, click on the first result google returns, click on "user guide", click on "Serializing and Deserializing generic types", read the solution to your problem. Commented Oct 27, 2013 at 12:50
  • Where is class JsonTest? please post it Commented Oct 27, 2013 at 12:51
  • I have posted JsonTest. Can u plz see it. Commented Oct 27, 2013 at 13:01

1 Answer 1

5

Add TypeToken:

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

Use Token if you want to serialize/de the 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}]}
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to accept the answer, is a another good way to say "thank you" :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.