Java后台对JSON格式的处理操作(一)


 1.将对象转换为JSON字符串,返回值为一个JSON字符串

public static String toJson(Object value) {

try {

return mapper.writeValueAsString(value);

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

2. JSON字符串转换为实体对象,返回值为实体对象

 

public static <T> T toObject(String json, Class<T> valueType) {

Assert.hasText(json);

Assert.notNull(valueType);

try {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

mapper.setDateFormat(dateFormat);

return mapper.readValue(json, valueType);

} catch (Exception e) {

e.printStackTrace();

}

return null;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM