最初的问题是您声明层次结构的方式。 A ListEntry
是Map<String, ListEntryInfo>
,但没有Map<String, ListEntryInfo>
。为了使它工作,你有三种选择:
声明ListEntry
类为class ListEntry extends HashMap<String, ListEntryInfo> {}
,这是
在我看来是一个坏主意摆脱ListEntry
类,并宣布这样@SerializedName("list") List<Map<String, ListEntryInfo>> entries;
的entries
列表
- 使用I最初低于所描述的方法,通过实现自定义串并转换器
如前所述,你可以做的是写一个自定义的反序列化器,这样你就可以得到更多类型的条目列表。
作为ListEntry
实例有映射到一个关键的只有一个ListEntryInfo
价值,我会结构改成这样:
class ListEntry {
private String key;
private ListEntryInfo value;
public ListEntry(String key, ListEntryInfo value) {
this.key = key;
this.value = value;
}
public String toString() {
return key + " -> " + value;
}
}
class ListEntryInfo {
//assuming we store the date as a String for simplicity
@SerializedName("date")
private String date;
public ListEntryInfo(String date) {
this.date = date;
}
public String toString() {
return date;
}
}
现在,你需要写一个解串器,当你反序列化创建一个新的ListEntry
实例JSON:
class ListEntryDeserializer implements JsonDeserializer<ListEntry> {
@Override
public ListEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Iterator<Map.Entry<String, JsonElement>> ite = json.getAsJsonObject().entrySet().iterator();
//you may want to throw a custom exception or return an "empty" instance there
Map.Entry<String, JsonElement> entry = ite.next();
return new ListEntry(entry.getKey(), context.deserialize(entry.getValue(), ListEntryInfo.class));
}
}
这解串器将读取每个ListEntry
。由于它由单个键值对组成(在第一种情况下,字符串“000000”映射到一个ListEntryInfo
等),我们获取密钥并将对应的ListEntryInfo
与JsonDeserializationContext
实例反序列化。
的最后一步,是将语法分析程序中进行注册:
Gson gson = new GsonBuilder().registerTypeAdapter(ListEntry.class, new ListEntryDeserializer()).create();
运行它在你的榜样,你应该得到:
[000000 -> 2015-07-10 14:29:15, 111111 -> 2015-07-11 14:29:15]
我稍稍偏出你的问题的一个关键点。我编辑了我的答案。 –