json工具处理对象属性时,该属性需要特殊处理时,例如时间本来是yyyy/MM/dd你想转换为yyyy-MM-dd

来源:互联网 发布:淘宝摄影棚实景布置图 编辑:程序博客网 时间:2024/05/09 09:05
第一步、时间格式化工具
public static synchronized String getDateSecondFormat(java.util.Date date) {return getDateFormat(date, "yyyy-MM-dd");}
</pre><pre code_snippet_id="541609" snippet_file_name="blog_20141204_3_1696394" name="code" class="java">第二步、自定义json的处理日期转换类
</pre><pre code_snippet_id="541609" snippet_file_name="blog_20141204_5_7043101" name="code" class="java">public class CustomJsonDateSerializer extends JsonSerializer<Date> {/* * (non-Javadoc) *  * @see org.codehaus.jackson.map.JsonSerializer#serialize(java.lang.Object, * org.codehaus.jackson.JsonGenerator, * org.codehaus.jackson.map.SerializerProvider) */@Overridepublic void serialize(Date value, JsonGenerator jgen,SerializerProvider provider) throws IOException,JsonProcessingException {jgen.writeString(DateUtil.getDateSecondFormat(value));}}

第三步、在对象属性中加入自定义json的处理日期转换类
/** * @return the modifyDate */@JsonSerialize(using = CustomJsonDateSerializer.class)public Date getModifyDate() {return modifyDate;}
这样就完成json处理对象返回自定义属性了
                                             
0 0