package com.jeecms.common.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* 简单实体转json
* @author yd
* @date 2019/2/25
*/
public class EntityConversionJson {
private final static JsonConfig jsonConfig = new JsonConfig();// 时间格式化
private EntityConversionJson() {}
//实体转json
public static JSONArray getJsonData(Object... entity) {
jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
JSONArray jsonArray = JSONArray.fromObject(entity, jsonConfig);
return jsonArray;
}
/**
* json日期数据转换
* @author john
*/
static class JsonDateValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd HH:mm:ss";
public JsonDateValueProcessor() {
super();
}
public JsonDateValueProcessor(String format) {
super();
this.format = format;
}
@Override
public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) {
return process(paramObject);
}
@Override
public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) {
return process(paramObject);
}
private Object process(Object value) {
if (value instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
return sdf.format(value);
}
return value == null ? "" : value.toString();
}
}
}