我有以下JSON对象,我想使用Google的GSON库反序列化它。不幸的是,我无法正确地获取列表。 GSON找到第一个列表条目,但不是第二个。使用GSON反序列化列表
这是我用来调用代码GSON:
Mentions result = gson.fromJson(response, Mentions.class);
这里是我的JSON文件:
{
"mentions": [
{
"allEntities": [
{
"kbIdentifier": "YAGO:Bob_Dylan",
"disambiguationScore": "0.63692"
}
],
"name": "Dylan",
"bestEntity": {
"kbIdentifier": "YAGO:Bob_Dylan",
"disambiguationScore": "0.63692"
}
},
{
"name": "Duluth",
"bestEntity": {
"kbIdentifier": "YAGO:Duluth\\u002c_Minnesota",
"disambiguationScore": "0.63149"
}
}
]
}
这些都是我所创建的普通Java对象:
public class Mentions {
public List<Mention> mentions = new ArrayList<>();
}
public class Mention {
@SerializedName("bestEntity")
public BestEntity entity;
@SerializedName("name")
public String name;
}
public class BestEntity {
@SerializedName("kbIdentifier")
public String kbIdentifier;
@SerializedName("disambiguationScore")
public Double disambiguationScore;
}
我也尝试直接反序列化列表,但它只是给了我一个错误,说GSON希望列表启动一个t输入的开始。
Type datasetListType = new TypeToken<Collection<Mention>>() {
}.getType();
List<Mention> mentions = gson.fromJson(response, datasetListType);
你可以发布你与你想要的结果,结果呢? – Tunaki
难道你不会错过'allEntities'的一些映射吗? –
所有实体均不包含相关信息。真正的JSON文件包含更多不相关的信息,我只是忘记从最小的例子中删除AllEntities。 当然,如果我稍后决定需要AllEntities,我会添加一个映射。 – icehawk