java - Json 对象保存为 String 而不是 Object
问题描述
我正在使用 Json/Gson 保存和加载哈希图。
当我使用键 String 和 Value EnumElement 保存哈希图属性时。例如:
private static enum Rank {
BOSS;
}
public static void main(String[] args){
HashMap<Object, Object> attributes = new HashMap<>();
attributes.put("test", Rank.BOSS);
//save json file here...
}
然后在使用 json 加载 hashmap 时,并像这样设置地图:
attributes = loadJson("path/savedAttributes");
然后加载的地图已经把Rank.BOSS的枚举元素改成了String“BOSS”
结果现在是:
"attributes": {
"test": "BOSS",
},
它现在是一个字符串。
我该如何克服呢?
我希望它再次加载为枚举元素而不是字符串表示
解决方案
尝试在TypeAdapterFactory
这里使用 Gson
推荐阅读
- gstreamer - 使用 GStreamer (RTSP) 流式传输网络摄像头
- sql - 如何为以下信息编写 SQL 查询
- python - 使用 pandas read_csv 函数将“真”保留为字符串,将“真”保留为布尔值
- angular - 带有旁观者的角度单元测试 - 模块“DynamicTestModule”导入的意外值“FormBuilder”。请添加@NgModule 注释
- c# - 在同步方法中进行 I/O 操作时,它是否总是产生一个新线程?
- biztalk - 是否可以在 BizTalk 2016 映射中使用多个表循环 Functoid?
- android - 带有 RecyclerView 和其他视图的 BottomSheet
- java - 调用改造没有价值
- hadoop - Namenode没有格式化就无法启动
- c# - 如何在 WPF 中使用 Helix 工具包 MeshBuilder 创建拉伸几何体?