json格式转换工具类
package com.briup.cms.common.util;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class JsonUtil {
//1把对象转换为json对象
public static String getJsonObjectFromObject(Object o){
JSONObject jsonObject = JSONObject.fromObject(o);
return jsonObject.toString();
}
//1把对象转换为json对象
//2可以进行数据的过滤或者处理
//例如: 这样可以让日期对象按照自己设置的格式输出
//JsonConfig jsonConfig = new JsonConfig();
//jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor());
public static String getJsonObjectFromObject(Object o,JsonConfig jsonConfig){
JSONObject jsonObject = JSONObject.fromObject(o,jsonConfig);
return jsonObject.toString();
}
//1把对象转换为json对象
//2可以设置需要忽略的字段
public static String getJsonObjectFromObject(Object o,String[] str){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setExcludes(str);
JSONObject jsonObject = JSONObject.fromObject(o,jsonConfig);
return jsonObject.toString();
}
//既能处理日期,又能处理映射环
public static String getJsonObjectFromObject1(Object o,String[] str,JsonConfig jsonConfig){
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setExcludes(str);
JSONArray jsonObject = JSONArray.fromObject(o,jsonConfig);
return jsonObject.toString();
}
//1把对象转换为json对象
//2可以设置需要忽略的字段
//3可以需要替换的字段
public static String getJsonObjectFromObject(Object o,String[] str,Map<String,String> map){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setExcludes(str);
JSONObject jsonObject = JSONObject.fromObject(o,jsonConfig);
String s = jsonObject.toString();
for(String k:map.keySet()){
String v = map.get(k);
s = s.replaceAll(k, v);
}
return s;
}
//1把对象转换为json数组
public static String getJsonArrayFromObject(Object o){
JSONArray array = JSONArray.fromObject(o);
return array.toString();
}
//1把对象转换为json数组
//2可以进行数据的过滤或者处理
public static String getJsonArrayFromObject(Object o,JsonConfig jsonConfig){
JSONArray array = JSONArray.fromObject(o,jsonConfig);
return array.toString();
}
//1把对象转换为json数组
//2可以设置需要忽略的字段
public static String getJsonArrayFromObject(Object o,String[] str){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setExcludes(str);
JSONArray array = JSONArray.fromObject(o,jsonConfig);
return array.toString();
}
//1把对象转换为json数组
//2可以设置需要忽略的字段
//3可以设置需要替换的字段
public static String getJsonArrayFromObject(Object o,String[] str,Map<String,String> map){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setExcludes(str);
JSONArray array = JSONArray.fromObject(o,jsonConfig);
String s = array.toString();
for(String k:map.keySet()){
String v = map.get(k);
s = s.replaceAll(k, v);
}
return s;
}
}
Web工具类,response writer流
package com.briup.cms.common.util;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
/**
* 常用工具类
*/
public class WebUtil {
/**
* 发送字符串
*/
public static void sendResponse(final String text) {
HttpServletResponse response = ServletActionContext.getResponse();
write(text, response);
}
/**
* 将字符串写到response writer流中
*/
private static void write(final String context,
final HttpServletResponse response) {
PrintWriter writer = null;
try {
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
writer = response.getWriter();
writer.write(context);
writer.flush();
} catch (IOException e) {
} finally {
if (writer != null) {
writer.close();
}
}
}
/**
* 返回response
*/
public static HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
* 返回request
*/
public static HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
}
下面这个action实现的功能是将articl bean中的category中的articles 过滤掉,防止在将articles 转换为json格式时引发死循环,因为article bean和category bean是互相关联的,article bean中有category,category中有articles,在转换为json格式,json工具包在转换时遍历article中的所有属性,其中包括category,在遍历category属性时遍历category的所有属性,包括articles,然后再遍历article所有属性,进入死循环。
改action还实现了将articles 的所有时间数据都转换为yyyy-MM-dd格式
@Action("articleFindAll")
public void articleFindAll() {
String json = null;
try {
List<Article> articles = articleService.findAll();
String[] strings = { "articles" };
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
@Override
public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
if(arg1==null){
return null;
}
return dateFormat.format(arg1);
}
@Override
public Object processArrayValue(Object arg0, JsonConfig arg1) {
return null;
}
});
json = JsonUtil.getJsonObjectFromObject1(articles, strings, jsonConfig);
WebUtil.sendResponse(json);
} catch (ServiceException e) {
e.printStackTrace();
json = null;
WebUtil.sendResponse(json);
}
}