Deserializing arbitrary object json arrays in inner classes with Gson or nested inner class
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):
// 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
}
]
}
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):
public class Container
{
private String context;
private Double cpuUsage;
private String name;
private RateContainer rates;
public Container()
{
}
}
RateContainer (inner static class to class Container, according to Gson specs):
public static class RateContainer
{
private List<DefinitionContainer> definition;
private List<RowsContainer> rows;
public static class DefinitionContainer
{
String key;
String type;
public DefinitionContainer()
{
}
}
public static class RowsContainer
{
String name;
Double rate;
public RowsContainer()
{
}
}
public RateContainer()
{
}
}
To parse the Json string, I use:
Container container = gson.fromJson(containerString, Container.class);
and I get the following exception:
Expecting object found: [{"key":"name","type":"string"},{"key":"rate","type":"double"}]
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:
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
so I thought that maybe I could get these arrays first, using something like:
JsonElement element = contain开发者_运维技巧erJsonElement.getAsJsonObject().get("rates");
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?
Thank you all in advance!
In response to a few things in the original question, note the following three things:
- Gson does not require deserialization to static inner classes.
- It's only necessary to use a generified
TypeToken
if the type to be deserialized to is a generified collection. If the type to be deserialized to only contains such a collection, then use of a generifiedTypeToken
is not necessary. - 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
JsonElement
can be avoided.
With the example JSON corrected as
{
"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"
}
]
}
}
...then the following deserializes (and serializes) simply and as expected.
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<Definition> definition;
private List<Row> rows;
}
class Definition
{
private String key;
private String type;
}
class Row
{
private String name;
private String rate;
}
精彩评论