title: How to convert complex Java object into JSON
body: <p>I'm trying to convert a complex Java object into JSON. But I'm not able to do so.
  I am using Java generics.</p>
  
  <p>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. </p>
  
  <pre><code>class ListRow&lt;T&gt;{
  
      private int total;
      private int currentRow;
      private List&lt;T&gt; test;
  
  
  public ListRow(int total, int currentRow, List&lt;T&gt; 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&lt;T&gt; getTest() {
      return test;
  }
  public void setTest(List&lt;T&gt; 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&lt;JsonTest&gt; list = new ArrayList&lt;JsonTest&gt;();
  list.add(test);
  list.add(test2);
  list.add(test3);
  list.add(test4);
  
  Gson gson = new Gson();
  System.out.print(gson.toJson(list));
  </code></pre>
  
  <p>When I run this program with this values, I got the correct output:</p>
  
  <pre><code> [{"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}]
  </code></pre>
  
  <p>But when I write the following code to achieve generic feature of Java, I do not get the desired output: </p>
  
  <pre><code>Gson gson = new Gson();
  ListRow&lt;JsonTest&gt; dataList = new ListRow&lt;JsonTest&gt;(2, 2, list);
  System.out.print(gson.toJson(dataList));
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>{"total":2,"currentRow":2,"test":[{},{},{},{}]}. 
  </code></pre>
  
  <p>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. </p>
  
  <p>Json Test case: </p>
  
  <pre><code>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&lt;JsonTest&gt; list = new ArrayList&lt;JsonTest&gt;();
      list.add(test);
      list.add(test2);
      list.add(test3);
      list.add(test4);
  
      Gson gson = new Gson();
      System.out.print(gson.toJson(list));
  
      ListRow&lt;JsonTest&gt; dataList = new ListRow&lt;JsonTest&gt;(2, 2, list);
      System.out.print(gson.toJson(dataList));
  
  }
  </code></pre>