package com.messoft.service.excel;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;
import com.messoft.utils.constant.ConfigConstant;
/**
* json-lib 辅助类
*
* @author 张秀皎
* @version 1.0, 2010-11-17
*/
public class JsonUtils {
public static JsonConfig configJson(String[] excludes, String datePattern) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonUtils.DateJsonValueProcessor(datePattern));
return jsonConfig;
}
public static class DateJsonValueProcessor implements JsonValueProcessor {
private SimpleDateFormat sdf;
public DateJsonValueProcessor() {
sdf = new SimpleDateFormat(ConfigConstant.DATE_STYLE_SHOT);
}
public DateJsonValueProcessor(String datePattern) {
sdf = new SimpleDateFormat(datePattern);
}
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return process(value);
}
private Object process(Object value) {
return sdf.format((Date) value);
}
}
}
用法:JSONObject jsonObject = JSONObject.fromObject(starvingProxy,JsonUtils.configJson(null, "yyyy-MM-dd"));
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;
import com.messoft.utils.constant.ConfigConstant;
/**
* json-lib 辅助类
*
* @author 张秀皎
* @version 1.0, 2010-11-17
*/
public class JsonUtils {
public static JsonConfig configJson(String[] excludes, String datePattern) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonUtils.DateJsonValueProcessor(datePattern));
return jsonConfig;
}
public static class DateJsonValueProcessor implements JsonValueProcessor {
private SimpleDateFormat sdf;
public DateJsonValueProcessor() {
sdf = new SimpleDateFormat(ConfigConstant.DATE_STYLE_SHOT);
}
public DateJsonValueProcessor(String datePattern) {
sdf = new SimpleDateFormat(datePattern);
}
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return process(value);
}
private Object process(Object value) {
return sdf.format((Date) value);
}
}
}
用法:JSONObject jsonObject = JSONObject.fromObject(starvingProxy,JsonUtils.configJson(null, "yyyy-MM-dd"));