gson ,将json转换成Map

来源:互联网 发布:java string 相等判断 编辑:程序博客网 时间:2024/05/22 13:57
Map<String, String> myMap = new HashMap<String, String>();myMap.put("one", "hello");myMap.put("two", "world");Gson gson = new GsonBuilder().create();String json = gson.toJson(myMap);System.out.println(json);Type typeOfHashMap = new TypeToken<Map<String, String>>() { }.getType();Map<String, String> newMap = gson.fromJson(json, typeOfHashMap); // This type must match TypeTokenSystem.out.println(newMap.get("one"));System.out.println(newMap.get("two"));
from http://stackoverflow.com/questions/8360836/gson-is-there-an-easier-way-to-serialize-a-map
0 0
原创粉丝点击