1 添加maven依赖
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.0</version> </dependency>
2 使用Gson
public static void test(){ //定义变量 String jsonString = "{" + "'A':'a'," + "'B':'b'," + "'C':'c'," + "'D':'d'," + "'E':'e'," + "'F':'f'," + "'G':'g'," + "'H':'h'," + "'over':'over'" + "}"; Gson gson = new Gson(); //gson 转换为map Map<String, String> retMap = gson.fromJson(jsonString, new TypeToken<Map<String, String>>(){}.getType()); //遍历 for (Map.Entry<String, String> entry : retMap.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } }