Data retrieved from json output saved into arrays using Gson
<p>I am building a restful web service and also a java client that consumes the web service. The responses from the server sent to the client are in json format. I want to store the attributes and the data from the json string to an array, so I can use the data to build some client operations. Let 's say I have I have a collection of products. The output from the rest server is in this format, and I simply have it in a string variable: </p>

<pre><code>String output="{"product":[{"code":"7","name":"book","value":"4.0"},{"code":"3","name":"pen","value":"4.5"},{"code":"9","name":"pencil","value":"5.5"}]}"
</code></pre>

<p>I have tried to convert the json object to a java object of type Product</p>

<pre><code>Gson gson = new GsonBuilder().serializeNulls().create();
Product product= gson.fromJson(output,Product.class);
System.out.println(product);
</code></pre>

<p>The output is:</p>

<pre><code>product:[product:null,name:book,code:7,value:4,000000,       
product:null,name:pen,code:3,value:4,500000,  
product:null,name:pencil,code:9,value:5,500000],name:null,code:null,value:null
</code></pre>

<p>and when I </p>

<pre><code>System.out.println(product.product);
</code></pre>

<p>the output is:</p>

<pre><code>[product:null,name:book,code:7,value:4,000000,     
product:null,name:pen,code:3,value:4,500000,   
product:null,name:pencil,code:9,value:5,500000]
</code></pre>

<p>First question: I don t know why the null values appear, and I can t find a way that I can fix this.</p>

<p>Second question: I have been searching around for days, but I really can t find a way to save these data into an array or a list. I don t want to save them in a java Products object. What I want to do is to have all the attributes together, for example an array of all the values, or the names or all the products. </p>

<pre><code>Something like this I have in mind:

String[] names=["book","pen","pencil"];
Integer[] codes=[7,3,9];
Double[] values=[4.0,4.5,5.5];
</code></pre>

<p>Third question: Should I make these operations (for example calculating the total values) in the server side? Will this be the better way to go?</p>

<p>Fourth question: Is it necessary to duplicate the Products class in the Client? That 's the reason I am not sure that the operations beyond CRUD should absolutely be on the client 's side.</p>

<p>Product class:</p>

<pre><code>class Product  {

public String name;
public Integer code;
public Double value;
public List&lt;Product&gt; product;


public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name=name;
}

public Integer getCode()
{
    return code;
}

public void setCode(Integer code)
{
    this.code=code;
}

public Double getValue()
{
    return value;
}

public void setValue(Double value)
{
    this.value=value;
}
public List&lt;Product&gt; getProduct() 
{ 
    return product; 
}
 public void setProduct(List&lt;Product&gt; order) 
 { 
     this.product = product; 
 }
@Override
public String toString() {
 return    String.format("product:%s,name:%s,code:%d,value:%f",product,name,code,value);
      }
}
</code></pre>

<p>I am using Gson library, because it seems to be the most simple way. </p>
