{"time2":"2018-12-31","mp":{"num":200,"time":"2018-12-19"},"num":11,"time":"2018-12-19"} ------------------------------------ Exception in thread "main" net.sf.json.JSONException: java.lang.reflect.InvocationTargetException at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818) at net.sf.json.JSONObject._fromBean(JSONObject.java:699) at net.sf.json.JSONObject.fromObject(JSONObject.java:172) at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274) at net.sf.json.JSONObject._processValue(JSONObject.java:2655) at net.sf.json.JSONObject.processValue(JSONObject.java:2721) at net.sf.json.JSONObject.setInternal(JSONObject.java:2736) at net.sf.json.JSONObject.setValue(JSONObject.java:1424) at net.sf.json.JSONObject._fromMap(JSONObject.java:1168) at net.sf.json.JSONObject.fromObject(JSONObject.java:163) at com.lethez.JsonLibTest.main(JsonLibTest.java:39) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2155) at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1323) at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762) at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837) at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426) at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:749) ... 10 more Caused by: java.lang.IllegalArgumentException at java.sql.Date.getHours(Date.java:187) ... 20 more
Process finished with exit code 1
测试代码
package com.lethez;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* author: lethez
* describe:
* date: 2018/12/19 - 9:21
*/
public class JsonLibTest {
public static void main(String[] args) {
Map map = new HashMap();
map.put("time",new Date());
map.put("time2",new Date(new Date().getTime()+1000000000));
map.put("num",11);
Map map2 = new HashMap();
map2.put("time",new Date());
map2.put("num",200);
map.put("mp",map2);
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(Date.class, new CustomerJsonValueProcessor("yyyy-MM-dd"));
JSONObject jo = JSONObject.fromObject(map,config);
System.out.println(jo);
System.out.println("------------------------------------");
Map map3 = new HashMap();
//map3.put("time",new java.sql.Date(100000));
map3.put("time",new Date((new java.sql.Date(100000)).getTime()));
JsonConfig config2 = new JsonConfig();
config2.registerJsonValueProcessor(Date.class,
new CustomerJsonValueProcessor2("yyyy-MM-dd"));
JSONObject jo2 = JSONObject.fromObject(map3,config);
System.out.println(jo2);
}
static class CustomerJsonValueProcessor implements JsonValueProcessor{
private String pattern;
public Object processArrayValue(Object o, JsonConfig jsonConfig) {
return process(o);
}
@Override
public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) {
return process(o);
}
public CustomerJsonValueProcessor() {
super();
}
public CustomerJsonValueProcessor(String format) {
this.pattern = format;
}
private Object process(Object val){
if(null!=val && val instanceof Date){
SimpleDateFormat sdf =
new SimpleDateFormat(this.pattern, Locale.CHINESE);
return sdf.format(val);
}
return val==null?"":val.toString();
}
}
static class CustomerJsonValueProcessor2 implements JsonValueProcessor{
private String pattern ;
public CustomerJsonValueProcessor2() {
super();
}
public CustomerJsonValueProcessor2(String format) {
this.pattern = format;
}
@Override
public Object processArrayValue(Object o, JsonConfig jsonConfig) {
return process(o);
}
@Override
public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) {
return process(o);
}
private Object process(Object val){
if(null!=val && val instanceof Date){
SimpleDateFormat sdf = new SimpleDateFormat(this.pattern,Locale.CHINESE);
return sdf.format(val);
}
return val==null?"":val.toString();
}
}
}