软件编程
位置:首页>> 软件编程>> java编程>> 浅谈Java后台对JSON格式的处理操作

浅谈Java后台对JSON格式的处理操作

作者:jingxian  发布时间:2023-02-16 07:28:36 

标签: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;
0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com