JSONArray JSONObject 转换时出错java.lang.reflect.InvocationTargetException

阅读更多
    在开发中经常会遇到把一个对象转换成JSON时提示转换错误,这个原因一般是JSONArray或JSONObject无法正常转换对象中的类型造成的,这个时候可以考虑把实体类中的所有属性名称打出来进行观察,如果发现有不是常规的类型就必须指定转换。



1.查看对象中的所有属性类型,如我现在已有一个Map temp对象

for(String s : temp.keySet()){
				try {
					log.error("-----------"+temp.get(s).getClass().getName());
				} catch (Exception e) {
					//e.printStackTrace();
				}
			}



打印出来发现如下
2013-11-11 15:49:39,801 ERROR (ReportPzAction.java:1731) --- -----------java.sql.Timestamp
2013-11-11 15:49:39,802 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,802 ERROR (ReportPzAction.java:1731) --- -----------org.hibernate.lob.SerializableClob
2013-11-11 15:49:39,802 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,803 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,803 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,803 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,804 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,804 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,804 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,805 ERROR (ReportPzAction.java:1731) --- -----------org.hibernate.lob.SerializableClob
2013-11-11 15:49:39,805 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,805 ERROR (ReportPzAction.java:1731) --- -----------org.hibernate.lob.SerializableClob
2013-11-11 15:49:39,806 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,806 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,806 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,807 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,807 ERROR (ReportPzAction.java:1731) --- -----------java.math.BigInteger
2013-11-11 15:49:39,807 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String
2013-11-11 15:49:39,808 ERROR (ReportPzAction.java:1731) --- -----------java.lang.String


其中的org.hibernate.lob.SerializableClob是数据库中的大字段类型,还有一个是java.sql.Timestamp日期类型,这两种类型无法直接进行JSON转换,需要设置对应的转换方式





2.设置转换代码
//创建转换说明对象
			JsonConfig config = new JsonConfig();
			
			//设置java.sql.Timestamp对应的转换方法
			config.registerJsonValueProcessor(java.sql.Timestamp.class,new JsonValueProcessor(){
				@Override
				public Object processArrayValue(Object value, JsonConfig config) {
					return process(value);
				}
				
				@Override
				public Object processObjectValue(String arg0, Object value,
						JsonConfig arg2) {
					return process(value);
				}
				
				private Object process(Object value) {
			        try {
			        	
			            if (value instanceof java.sql.Timestamp) {
			                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			                System.out.println("sdf.format( value):"+sdf.format( value));
			                return sdf.format( value);
			            }
			            return value == null ? "" : value.toString();
			        } catch (Exception e) {
			        	e.printStackTrace();
			            return "";
			        }

			    }
			});
			
			//设置org.hibernate.lob.SerializableClob对应的转换方法
			config.registerJsonValueProcessor(org.hibernate.lob.SerializableClob.class,new JsonValueProcessor(){
				@Override
				public Object processArrayValue(Object value, JsonConfig config) {
					return process(value);
				}
				
				@Override
				public Object processObjectValue(String arg0, Object value,
						JsonConfig arg2) {
					return process(value);
				}
				
				private Object process(Object value) {
					try {
						
						if (value instanceof java.sql.Timestamp) {
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
							System.out.println("sdf.format( value):"+sdf.format( value));
							return sdf.format( value);
						}
						return value == null ? "" : value.toString();
					} catch (Exception e) {
						e.printStackTrace();
						return "";
					}
					
				}
			});


			
			JSONObject object = JSONObject.fromObject(pager,config);

你可能感兴趣的:(java,json)