今天在项目中使用了
1 PageGridUtil util = new PageGridUtil(request); 2 3 JsonConfig config = new JsonConfigBuilder().createJsonConfig(new String[] {}); 4 5 JSONObject summaryJson = JSONObject.fromObject(vo); 6 7 JSONObject pageObj = new JSONObject(); 8 9 pageObj.put("data1", summaryJson); 10 11 util.renderJSON(response, pageObj.toString());
来封装JSON对象
在封装对象JSONObject.fromObject(vo)时发生了一下异常:
1 net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 2 net.sf.json.JSONObject._fromBean(JSONObject.java:959)
网上查了一下,发现是:在hibernate配置文件中用到了:
<property name="shenqingriqi"type="date"></property>
可能问题一:
JSON对象把date认为是java.sql.Date
解决方案:
把hibernate配置文件改为:
<property name="shenqingriqi"type="java.util.Date"></property>
这样应该就可以了。
可能问题二:
在封装对象时有可能hibernate级联到了别的对象,使用到了延迟加载而导致了这个问题
解决方案:
PageGridUtil util = new PageGridUtil(request);
JsonConfig config = new JsonConfigBuilder().createJsonConfig(new String[] {"handler","hibernateLazyInitializer"});
JSONObject summaryJson = JSONObject.fromObject(vo, config);
JSONObject pageObj = new JSONObject();
pageObj.put("data1", summaryJson);
util.renderJSON(response, pageObj.toString());