写这边文章的初衷就是遇到了json在转换日期格式出现了异常 net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 原因是当你用Map接收数据库返回了java.sql.Date 日期的数据进行json转换出的问题话不多说 直接上代码
经过测试,以下json对日期的格式化转换支持java.util.Date,java.sql.Date 或者时间戳 等等日期格式
转化方法
/** * toString的另一种写法 * 通过传入参数日期的类类型 * @parm Class<?> c 日期的类类型 如java.util.Date.class * @parm String format 日期格式化设置 如 "yyyy-mm-dd" * @return String */ public String getString(Class<?> c,String format){ //如果传入的类类型不是日期的类类型 就给默认的类型 if(!"Date".equals(c.getSimpleName())){ c = java.util.Date.class; } if(null == c || "".equals(c)&& !"Timestamp".equals(c.getSimpleName())){ c = java.util.Date.class; } //创建一个格式化日期json对象 JsonConfig entryConfig = new JsonConfig(); // 将java.util.Date()的日期格式转换并且设置转换的格式(实体对象) if(null == format || "".equals(format)){ format = "yyyy-MM-dd"; } entryConfig.registerJsonValueProcessor(c, new JsonDateValueProcessor(format)); JSONObject jsonObject = new JSONObject(); if(null == rows || "".equals(rows)){ jsonObject.put("rows", ""); }else{ //如果是集合则用jsonarry转换 if(rows instanceof java.util.List) jsonObject.put("rows", JSONArray.fromObject(rows,entryConfig)); else jsonObject.put("rows", JSONObject.fromObject(rows,entryConfig)); } jsonObject.put("total", total); jsonObject.put("statusCode", statusCode); jsonObject.put("message", message); return jsonObject.toString(); }
格式化类
import java.text.SimpleDateFormat; import java.util.Date; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; public class JsonDateValueProcessor implements JsonValueProcessor { // 定义转换日期类型的输出格式 private String format = "yyyy-MM-dd"; public JsonDateValueProcessor() { } public JsonDateValueProcessor(String format) { this.format = format; } @Override public Object processArrayValue(Object arg0, JsonConfig arg1) { return process(arg0); } private Object process(Object arg0) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(arg0); } @Override public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if (value instanceof java.util.Date) { String str = new SimpleDateFormat(format).format((Date) value); return str; } if (null != value) { return value.toString(); } return ""; }
相关推荐
JsonObject转换Date日期的时候出现问题解决date封装成json数据出现的问题
JsonObject
引用using Xfrog.Net; 用法如: JsonObject newObj = new JsonObject("json字符串"); String errorCode = newObj["error_code"].Value;//获取其中的属性值。
JSONObject相关依赖架包,包含1、com.alibaba.fastjson.JSONArray依赖jar 2、net.sf.json.JSONObject依赖jar
jsonObject、jsonArray
org.json.JSONObject引用依赖包 org.json.JSONObject引用依赖包 org.json.JSONObject引用依赖包 org.json.JSONObject引用依赖包
JAVA JSONObject Jar包
net.sf.json.JSONObject Json处理的相关依赖包 导入即可用
文件名称:JSONObject.rar(可用其他解压器解压),内含文件若干(6个)如下 commons-beanutils-1.9.1.jar commons-collections-3.2.1.jar commons-lang-2.6.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-...
JSONObject用法 ,分享给大家
java解析json使用到的jar包 必须全
JsonObject jar包
使用JSONObject的所有包
Java开发当中经常设计到Json格式的数据使用,这就用到JSONObject类,依赖这些jar架包即可。
如果想要在Java中使用JSONObject,而且只想通过导入jar包的方式下,那么仅仅导入Json的jar包还是不够的。JSONObject完整所需jar包,注意版本问题。
包含JSONObject所需依赖的6个jar包 以及JSONObjectAPI文档 和 JSONObject源码 其中文档直接解压压缩包在文件夹中的index.html打开,源码在你的ide编辑器中引入就可以查看JSONObject实现源码