1.把处理好的list或map序列化成JSON字符
/** * 序列化集合成JSON字符 * @param list * @return */ public static String structureConfigParamsGroupJSONData(List<?> list) { JSONSerializer serializer = new JSONSerializer(); String json=""; json = serializer.exclude("*.class").deepSerialize(list).replaceAll(":\s*null\s*", ":"""); return json; } public static String structureConfigParamsGroupJSONData(Map<String, ?> map) { JSONSerializer serializer = new JSONSerializer(); String json=""; json = serializer.exclude("*.class").deepSerialize(map).replaceAll(":\s*null\s*", ":"""); return json; } 作者:littleDragon 链接:https://www.jianshu.com/p/c4068c941939 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2.输出JSON
/** * 输出JSON * * @param response * @param result * @throws IOException */ public void print(HttpServletResponse response, String result) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/json;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print(result); out.flush(); out.close(); } 作者:littleDragon 链接:https://www.jianshu.com/p/c4068c941939 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
另外,Gson 是google解析Json的一个开源框架,同类的框架fastJson,JackJson等等,也很好用。请自行百度,有很多参考文章。