ObjectMapper 类处理数据转换
Users users = new Users();
users.setAge(1);
users.setCountry("中国");
users.setId(1);
String s = JSON.toJSONString(users);
System.out.println(s);
//转义符一条斜杠
Gson gson = new Gson();
String jsonStr = gson.toJson(s);
System.out.println(jsonStr);
//转义符两条斜杠
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson1 = gsonBuilder.create();
String s1 = gson1.toJson(jsonStr);
System.out.println(s1);
//普通JSon字符串转化 方法一
//https://blog.csdn.net/xueyijin/article/details/124083389 还可以处理带 / 的转义字符串
ObjectMapper objectMapper = new ObjectMapper();
users = objectMapper.readValue(s, Users.class);
//普通JSon字符串转化 方法二 二用的居多
Users users1 = JSONObject.parseObject(s, Users.class);
System.out.println(users1);
//Json集合转集合
List<Users> list = new ArrayList<>();
list.add(users);
ObjectMapper objectMapperList = new ObjectMapper();
JavaType javaType = objectMapperList.getTypeFactory().constructParametricType(ArrayList.class, Users.class);
list = objectMapperList.readValue(JSON.toJSONString(list), javaType);
System.out.println("JavaType 转换为:"+list);