String to map in java, map to String, mutual conversion between map and String

String to map:

Map map_new = new Gson().fromJson(s, map.getClass());//Need to import jar package

but! Still can’t convert complex ones, such as map.put(“getAll=toMe”,”one”) , it will go wrong

referenced jar

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.2.4</version>
</dependency>

code to test

public static void main(String[] args) {
    Map map = new HashMap<String,String>();
    map.put("a","a111");
    map.put("a1","a111");
    map.put("a2","a111");
    String s = map.toString();
    Map map_new = new Gson().fromJson(s, map.getClass());
}

result of running

Leave a Comment

Your email address will not be published. Required fields are marked *