对JSONObject.fromObject 的处理
1.扩展JsonValueProcessor接口(注意对null的处理)
import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /*** * 将Bean中的Timestamp转换为json中的日期字符串 * @author dxswzj@163.com * */ public class DateJsonValueProcessor implements JsonValueProcessor { public static final String Default_DATE_PATTERN ="yyyy-MM-dd"; private DateFormat dateFormat ; public DateJsonValueProcessor(String datePattern){ try{ dateFormat = new SimpleDateFormat(datePattern);} catch(Exception e ){ dateFormat = new SimpleDateFormat(Default_DATE_PATTERN); } } public Object processArrayValue(Object value,JsonConfig jsonConfig) { if(null!=value){ return process(value); }else{ return ""; } } public Object processObjectValue(String key,Object value,JsonConfig jsonConfig) { if(null!=value){ return process(value); }else{ return ""; } } private Object process(Object value){ return dateFormat.format((Date)value); } }
2.转化时 JSONObject.fromObject 加上参数
Question question = questionService.selectQuestionById(id);
JsonConfig config=new JsonConfig(); config.registerJsonValueProcessor(Timestamp.class,new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss")); config.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); JSONObject json = JSONObject.fromObject(question,config);
这样json里得到的日期格式就是按我们定义的格式显示
对toBean()的处理
json日期格式可以是 2018-02-01 12:23:12 或 2014-08-08
1.编写一个Morpher类继承自AbstractObjectMorpher
class TimeStampMorpher extends AbstractObjectMorpher{ private Logger LOG = Logger.getLogger(TimeStampMorpher.class); private String[] formats = new String[]{"yyyy-MM-dd hh:mm:ss","yyyy-MM-dd"}; public void setFormats(String[] formats) { this.formats = formats; } public TimeStampMorpher() { // TODO Auto-generated constructor stub } public TimeStampMorpher(String[] formats) { this.formats = formats; } @Override public Object morph(Object dateStr) { // TODO Auto-generated method stub if(null == dateStr){ return null; } if(Timestamp.class.isAssignableFrom(dateStr.getClass())){ return (Timestamp)dateStr; } if(!supports(dateStr.getClass())){ throw new MorphException(dateStr.getClass()+"不是支持的类型!"); } String strValue = (String)dateStr; SimpleDateFormat dateParser = null; for(int i=0,k=formats.length;i<k;i++){ if(null == dateParser){ dateParser = new SimpleDateFormat(formats[i]); }else{ dateParser.applyPattern(formats[i]); } try{ return new Timestamp(dateParser.parse(strValue.toLowerCase()).getTime())); }catch(ParseException e){ e.printStackTrace(); } } return new java.sql.Timestamp(System.currentTimeMillis());//返回默认日期 } @Override public Class morphsTo() { // TODO Auto-generated method stub return Timestamp.class; } public boolean supports(Calss claszz){ return String.class.isAssignableFrom(claszz); } }在toBean的地方
JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher()); Question question = (Question)JSONObject.toBean(questionJson,Question.class);
随便提下 如果Question有分录的话需要这样转
Map<Stirng,Class> classMap = new HashMap<Stirng,Class>(); classmap.put("questionEntry",QuestionEntry.class); question = (Question)JSONObject.toBean(questionJson,Question.class,calssMap);
其中Question 有属性
private List<QuestionEntry> questionEntry;