当我尝试使用 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
}
]
}
现在,我遇到的问题是当我尝试反序列化 json 数组(“定义”和“行”)时。其余字段在反序列化中获得适当的值。我正在使用的类定义如下(为简单起见,没有 getter/setter):
public class Container
{
private String context;
private Double cpuUsage;
private String name;
private RateContainer rates;
public Container()
{
}
}
RateContainer(根据 Gson 规范,内部静态类到类 Container):
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()
{
}
}
要解析 Json 字符串,我使用:
Container container = gson.fromJson(containerString, Container.class);
我得到以下异常:
Expecting object found: [{"key":"name","type":"string"},{"key":"rate","type":"double"}]
看起来类定义中必须有一些不能正常工作的东西。我检查了 Gson API,我知道,为了反序列化列表,通常要做的是:
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
所以我想也许我可以先得到这些数组,使用类似的东西:
JsonElement element = containerJsonElement.getAsJsonObject().get("rates");
然后获取“定义”和“行”,但我更愿意将所有内容保留在 Container 对象中。有没有办法以这种方式反序列化这些列表?类定义有问题吗?
谢谢大家!