我在服务器上序列化数据:
Gson gson = new Gson();
Map<String, List<?>> resultMap = BackendUpdateManager.getInstance()
.getUpdates(timeHolder, shopIdInt, buyerIdInt);
gson.toJson(resultMap);
以及反序列化:
Gson gson = new Gson();
Map<String, List<?>> resultMap = gson.fromJson(json,
new TypeToken<Map<String, List<?>>>() {
}.getType());
然而,当我尝试使用
Map
中的项目时:List<ProductCategory> productCategoryList = (List<ProductCategory>)updateMap.get(key);
for(ProductCategory productCategory : productCategoryList) {
}
我遇到了错误:
由于:
java.lang.ClassCastException
:com.google.gson.internal.LinkedTreeMap
无法转换为com.example.model.entity.ProductCategory
我该如何修复这个错误或以其他方式创建一个具有List<不同类>
的Map
?
我尝试创建包含List
而非Map<String, List<?>>
的不同类的getter和setter,用于序列化和反序列化。但是我正在寻找更好的方法。
Map<String, List<ProductCategory>>
,为什么要将其反序列化为Map<String, List<?>>
? - azurefrogupdateMap.get(key)
。 - ViT-Vetal-