Gson jsonString Map object transfer

  1. //transfer jsonstring into  map
  2. private String jsonstrToMap() {
  3.     Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
  4.     Map<String, String> map = new HashMap<String, String>();
  5.     map.put(“key1”“value1”);
  6.     map.put(“key2”“value2”);
  7.     map.put(“key3”“value3”);
  8.     String jsonString = gson.toJson(map);
  9.     Type type = new TypeToken<Map<String, String>>() {}.getType();
  10.     Map<String, String> map2 = gson2.fromJson(jsonString, type);
  11.     String showString = “”;
  12.     for (String keyString : map2.keySet()) {
  13.         showString += keyString + “:” + map2.get(keyString) + “\n—–\n”;
  14.     }
  15.     showString += “———————-\n”;
  16.     return showString;
  17. }

reference: http://blog.csdn.net/a249900679/article/details/51386660

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.