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;
}
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();
}