Json字符串和对象的转换.txt

时间:2020-02-04 16:10:11
【文件属性】:
文件名称:Json字符串和对象的转换.txt
文件大小:782B
文件格式:TXT
更新时间:2020-02-04 16:10:11
Json字符串和对象 private final static ObjectMapper MAPPER = new ObjectMapper(); //用ObjectMapper工具类把对象转换为JSON字符串 public static String toJson(Object obj) { String result=""; try { result = MAPPER.writeValueAsString(obj); } catch (JsonProcessingException e) { e.printStackTrace(); //将编译异常 转换为运行时异常 出错就终止 不继续执行 回滚事务 throw new RuntimeException(e); } return result; } // 把字符串转换为我们传进来的对象类 public static T toObject(String obj,Class ct) { T t=null; try { t = MAPPER.readValue(obj, ct); } catch (JsonProcessingException e) { e.printStackTrace(); //将编译异常 转换为运行时异常 出错就终止 不继续执行 回滚事务 throw new RuntimeException(e); } return t; }

网友评论