1 arrays nested list gson deserialization
当我尝试用Gson反序列化一个Json字符串时遇到麻烦.字符串是这样的(注意:我只是简化了它,但是留下了我遇到麻烦的部分,因此,可能存在Json语法错误,但我已经通过在线验证器检查了我正在使用的字符串没关系):
// 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
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在,我得到的问题是当我尝试反序列化json数组("定义"和"行")时.其余字段在反序列化中获得适当的值.我正在使用的类定义如下(为简单起见,没有getter/setter):
public class Container
{
private String context;
private Double cpuUsage;
private String name;
private RateContainer rates;
public Container()
{
}
}
Run Code Online (Sandbox Code Playgroud)
RateContainer(内部静态类到类Container,根据Gson规范):
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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
要解析Json字符串,我使用:
Container container = gson.fromJson(containerString, Container.class);
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
Expecting object found: [{"key":"name","type":"string"},{"key":"rate","type":"double"}]
Run Code Online (Sandbox Code Playgroud)
看起来在类定义中必须有一些不能正常工作的东西.我检查了Gson API,我知道,为了反序列化列表,通常要做的是:
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
Run Code Online (Sandbox Code Playgroud)
所以我想也许我可以先使用以下方法获得这些数组:
JsonElement element = containerJsonElement.getAsJsonObject().get("rates");
Run Code Online (Sandbox Code Playgroud)
然后得到"定义"和"行",但我更喜欢将所有内容保存在Container对象中.有没有办法以这种方式反序列化这些列表?类定义中有什么问题吗?
谢谢大家!
在回答原始问题中的一些事项时,请注意以下三点:
TypeToken如果要反序列化的类型是一个通用的集合,则只需要使用generified.如果要反序列化的类型仅包含这样的集合,则TypeToken不需要使用generified .JsonElement可以避免明确使用组件.将示例JSON更正为
{
"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"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
...然后按照预期简单地反序列化(和序列化).
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;
}
Run Code Online (Sandbox Code Playgroud)