日期:2014-05-16 浏览次数:20322 次
/** * * @Description 将JAVA对象转换为JSON格式的字符串 升级版本 * @create Jul 5, 2010 * @auhtor 覃正平 * @param object : 对象 * @return String */ public static String convert2Json2(Object object){ JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor(){ private final String format = "yyyy-MM-dd hh:mm:ss" ; public Object processArrayValue(Object object, JsonConfig jsonConfig) { return null; } public Object processObjectValue(String string, Object object,JsonConfig jsonConfig) { if(null == object ){ return "" ; }else{ if(object instanceof java.util.Date){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format) ; String dateStr = simpleDateFormat.format(object) ; return dateStr ; } } return object.toString(); } }) ; if(object instanceof String) return object.toString(); if((object instanceof Object[])||(object instanceof List)){ JSONArray jsonObject = JSONArray.fromObject(object,jsonConfig); return jsonObject.toString()+'\n'; }else{ JSONObject jsonObject = JSONObject.fromObject(object,jsonConfig); return jsonObject.toString()+'\n'; } }