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
|
public class JsonUtils {
public static String toJsonStr(Object object) throws Exception { return new ObjectMapper().writeValueAsString(object); }
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(); } } }
|