Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing arbitrary object json arrays in inner classes with Gson or nested inner class
    primarykey
    data
    text
    <p>I am having troubles when I try to deserialize one Json string with Gson. The string goes something like this (Note: I just simplified it, but leaving the part I am having troubles with and because of that, there might be Json syntaxis errors, but I have checked with an online validator that the string I am working with is OK):</p> <pre><code>// let's call this "container" json element { "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>Now, the problem I get is when I try to deserialize the json arrays ("definition" and "rows"). Rest of fields get proper values in deserialization. The class definition I am using is the following (no getters/setters for simplicity):</p> <pre><code>public class Container { private String context; private Double cpuUsage; private String name; private RateContainer rates; public Container() { } } </code></pre> <p>RateContainer (inner static class to class Container, according to Gson specs):</p> <pre><code>public static class RateContainer { private List&lt;DefinitionContainer&gt; definition; private List&lt;RowsContainer&gt; rows; public static class DefinitionContainer { String key; String type; public DefinitionContainer() { } } public static class RowsContainer { String name; Double rate; public RowsContainer() { } } public RateContainer() { } } </code></pre> <p>To parse the Json string, I use:</p> <pre><code>Container container = gson.fromJson(containerString, Container.class); </code></pre> <p>and I get the following exception:</p> <pre><code>Expecting object found: [{"key":"name","type":"string"},{"key":"rate","type":"double"}] </code></pre> <p>Looks like there has to be something in the class definition that does not work well. I have checked the Gson API and I know that, in order to deserialize lists, the usual thing to do is: </p> <pre><code>Type collectionType = new TypeToken&lt;Collection&lt;Integer&gt;&gt;(){}.getType(); Collection&lt;Integer&gt; ints2 = gson.fromJson(json, collectionType); </code></pre> <p>so I thought that maybe I could get these arrays first, using something like:</p> <pre><code>JsonElement element = containerJsonElement.getAsJsonObject().get("rates"); </code></pre> <p>and then get "definition" and "rows", but I would prefer to keep everything in the Container object. Is there a way to deserialize these lists in such a way? Is there something wrong in the class definition? </p> <p>Thank you all in advance!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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