Google GSON nested HashMaps deserialization

前端 未结 1 856
旧巷少年郎
旧巷少年郎 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<String,Category>.class, but you can not do this in Java so the solution is to use what they called TypeToken in Gson.

    Map<String, Category> categoryMap = gson.fromJson(br, new TypeToken<Map<String, Category>>(){}.getType());
    
    0 讨论(0)
提交回复
热议问题