当前位置: 代码迷 >> J2EE >> struts2转化json有关问题
  详细解决方案

struts2转化json有关问题

热度:251   发布时间:2016-04-22 02:39:54.0
struts2转化json问题
 
String hql="from xxxx";
List<CCCCCCC> list=xxxx.getCPList(hql);  
try{
JSONArray json=JSONArray.fromObject(list);

jsonStr=json.toString();
}catch(Exception e){
  e.printStackTrace();
}
为什么这样转换就出现异常,CCCCCCC是个实体类里面有 时间类型。上网查了下好像是JSON转化时间的问题,要怎么解决,我自己也测试了,去掉时间的字段就一切正常了。下面是异常信息。
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at net.sf.json.JSONObject._fromBean(JSONObject.java:959)
at net.sf.json.JSONObject.fromObject(JSONObject.java:194)
at net.sf.json.JSONObject._processValue(JSONObject.java:2793)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2817)
at net.sf.json.JSONObject.setValue(JSONObject.java:1527)
at net.sf.json.JSONObject._fromBean(JSONObject.java:946)
at net.sf.json.JSONObject.fromObject(JSONObject.java:194)
at net.sf.json.JSONArray._processValue(JSONArray.java:2562)
at net.sf.json.JSONArray.processValue(JSONArray.java:2593)
at net.sf.json.JSONArray.addValue(JSONArray.java:2580)
at net.sf.json.JSONArray._fromCollection(JSONArray.java:1084)
at net.sf.json.JSONArray.fromObject(JSONArray.java:147)
at net.sf.json.JSONArray.fromObject(JSONArray.java:129)
at com.yx.action.TelCpAction.getListCp(TelCpAction.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

------解决方案--------------------
不要用日期类型,把日期类型转换成String试一下
------解决方案--------------------
Java code
    public String query() throws IOException {            sysparamList = doQuery();            ActionContext context = ActionContext.getContext();            HttpServletResponse response = (HttpServletResponse) context                    .get(ServletActionContext.HTTP_RESPONSE);            response.setHeader("Cache-Control", "no-cache");            response.setContentType("text/json;charset=UTF-8");            JSONArray json = JSONArray.fromObject(sysparamList);            String jsonarray = json.toString();            jsonarray = "{\"jsonarray\":" + jsonarray + "}";            System.out.println(jsonarray);            response.getWriter().write(jsonarray);            return null;    }
------解决方案--------------------
List 最好用JSONArray来转。
  相关解决方案