Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Gson has some limitations regarding collections because of Java's type erasure. You can read more about it <a href="http://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples" rel="noreferrer">here</a>.</p> <p>From your question I see you're using both <code>ArrayList</code> and <code>LinkedList</code>. Are you sure you didn't mean to use just <code>List</code>, the interface?</p> <p>This code works:</p> <pre><code>List&lt;String&gt; listOfStrings = new ArrayList&lt;String&gt;(); listOfStrings.add("one"); listOfStrings.add("two"); Gson gson = new Gson(); String json = gson.toJson(listOfStrings); System.out.println(json); Type type = new TypeToken&lt;Collection&lt;String&gt;&gt;(){}.getType(); List&lt;String&gt; fromJson = gson.fromJson(json, type); System.out.println(fromJson); </code></pre> <p><strong>Update</strong>: I changed your class to this, so I don't have to mess around with other classes:</p> <pre><code>class IndicesAndWeightsParams { public List&lt;Integer&gt; indicesParams; public List&lt;String&gt; weightsParams; public IndicesAndWeightsParams() { indicesParams = new ArrayList&lt;Integer&gt;(); weightsParams = new ArrayList&lt;String&gt;(); } public IndicesAndWeightsParams(ArrayList&lt;Integer&gt; indicesParams, ArrayList&lt;String&gt; weightsParams) { this.indicesParams = indicesParams; this.weightsParams = weightsParams; } } </code></pre> <p>And using this code, everything works for me:</p> <pre><code>ArrayList&lt;Integer&gt; indices = new ArrayList&lt;Integer&gt;(); ArrayList&lt;String&gt; weights = new ArrayList&lt;String&gt;(); indices.add(2); indices.add(5); weights.add("fifty"); weights.add("twenty"); IndicesAndWeightsParams iaw = new IndicesAndWeightsParams(indices, weights); Gson gson = new Gson(); String string = gson.toJson(iaw); System.out.println(string); IndicesAndWeightsParams fromJson = gson.fromJson(string, IndicesAndWeightsParams.class); System.out.println(fromJson.indicesParams); System.out.println(fromJson.weightsParams); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload