Google GSON nested HashMaps deserialization

前端 未结 1 847
旧巷少年郎
旧巷少年郎 2020-12-31 06:22

In my current project i use GSON library in android, and i\'ve faced a problem of nested Maps deserializtion. This is how initial json looks like

 {

\"5\":         


        
1条回答
  •  被撕碎了的回忆
    2020-12-31 07:19

    This gson.fromJson(br, HashMap.class); tells to Gson that you want to deserialize to a Map of unknown value type. You would be tempted to specifiy something like Map.class, but you can not do this in Java so the solution is to use what they called TypeToken in Gson.

    Map categoryMap = gson.fromJson(br, new TypeToken>(){}.getType());
    

    0 讨论(0)
提交回复
热议问题