咖啡仔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

今天在项目中使用了

 

 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级联到了别的对象,使用到了延迟加载而导致了这个问题

解决方案:

 1 PageGridUtil util = new PageGridUtil(request);
 2 
 3 JsonConfig config = new JsonConfigBuilder().createJsonConfig(new String[] {"handler","hibernateLazyInitializer"});
 4 
 5 JSONObject summaryJson = JSONObject.fromObject(vo, config);
 6 
 7 JSONObject pageObj = new JSONObject();
 8 
 9 pageObj.put("data1", summaryJson);
10 
11 util.renderJSON(response, pageObj.toString());

 

posted on 2012-07-03 10:23  咖啡仔  阅读(10550)  评论(0编辑  收藏  举报