02-JsonUtils

JasonUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* json 工具类
*/
public class JsonUtils {

/**
* 将 java 对象转换成 json 字符串
* @param object java对象
* @return json字符串
* @throws Exception
*/
public static String toJsonStr(Object object) throws Exception {
return new ObjectMapper().writeValueAsString(object);
}

/**
* 将 java 对象转换为 json 字符串,将 json 响应到浏览器
* @param response 响应对象
* @param object java对象
*/
public static void writeJsonStr(HttpServletResponse response, Object object) {
response.setContentType("application/json;charset=utf-8");
try {
String jsonStr = toJsonStr(object);
response.getWriter().write(jsonStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}

02-JsonUtils
https://janycode.github.io/2016/05/02/20_收藏整理/03_工具类/02-JsonUtils/
作者
Jerry(姜源)
发布于
2016年5月2日
许可协议