Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In response to a few things in the original question, note the following three things:</p> <ol> <li>Gson does not require deserialization to static inner classes.</li> <li>It's only necessary to use a generified <code>TypeToken</code> if the type to be deserialized to is a generified collection. If the type to be deserialized to only <em>contains</em> such a collection, then use of a generified <code>TypeToken</code> is not necessary.</li> <li>A major benefit of using an API like Gson or Jackson is for simple mapping and serialization/deserialization of Java data structures to/from JSON. So, explicite use of components like <code>JsonElement</code> can be avoided.</li> </ol> <p>With the example JSON corrected as</p> <pre><code>{ "context": "context", "cpuUsage": "cpuUsageValue", "name": "thename", "rates": { "definition": [ { "key": "name", "type": "string" }, { "key": "rate", "type": "double" } ], "rows": [ { "name": "thename1", "rate": "therate" }, { "name": "thename2", "rate": "therate2" } ] } } </code></pre> <p>...then the following deserializes (and serializes) simply and as expected.</p> <pre><code>import java.io.FileReader; import java.util.List; import com.google.gson.Gson; public class Foo { public static void main(String[] args) throws Exception { Gson gson = new Gson(); Container container = gson.fromJson(new FileReader("input.json"), Container.class); System.out.println(gson.toJson(container)); } } class Container { private String context; private String cpuUsage; private String name; private Rates rates; } class Rates { private List&lt;Definition&gt; definition; private List&lt;Row&gt; rows; } class Definition { private String key; private String type; } class Row { private String name; private String rate; } </code></pre>
 

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