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());
}
}
3 使用Gson
String roleList = "[{"id": "id1", "name": "roleName1"}, {"id": "id1", "name": "roleName1"}]";
JSONArray jsonArray = new JSONArray(roleList);