net.sf.json.JSONException: There is a cycle in the hierarchy!
struts2.1.8 中json格式转换问题:需要的7个包全部导入——只是commons-lang-2.4.jar 我用的是commons-lang-2.3.jar。这个应该没问题吧!
我的配置跟页面都没有问题~~~!因为下面的测试可以正常提交跟响应!
但是在执行这一句的时候(this.getMgrMeetingList()返回的是list集合):
JSONObject json = JSONObject.fromObject(this.getMgrMeetingList());
net.sf.json.JSONException: There is a cycle in the hierarchy!
//获取会议室编号 String meet = request.getParameter("meetId"); JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new PropertyFilter(){ public boolean apply(Object source, String name, Object value) { if(name.equals("tbmeetroom") || "tbmeetroom" == name || name.equals("tbmeettype") || "tbmeettype" == name ||name.equals("tbmeetfiles") || "tbmeetfiles" == name || name.equals("tbmeetpersons") || "tbmeetpersons" == name ) { return true; } else { return false; } } }); //如果会议编号不为空 if(null !=meet && !"".equals(meet)){ //创建获取星期日期对象 TimeTake time = new TimeTake(); //调用查询方法 this.setMgrMeetingList(mgrService.getMgrByTime(Long.valueOf(meet), time.getTime())); } //创建json对象 JSONObject json = JSONObject.fromObject(this.getMgrMeetingList(),config); result = json.toString(); System.out.println(result); return SUCCESS;
<many-to-one name="tbmeetroom" class="com.boxun.crm.dao.entities.Tbmeetroom" fetch="select"> <column name="HUIYISHI" precision="22" scale="0"> <comment>会议室,外键,和会议室管理主键ID对应</comment> </column> </many-to-one> <many-to-one name="tbmeettype" class="com.boxun.crm.dao.entities.Tbmeettype" fetch="select"> <column name="LEIXING" precision="22" scale="0"> <comment>会议类型,外键,和会议类型表的主键ID相关联</comment> </column> </many-to-one> <set name="tbmeetfiles" inverse="true"> <key> <column name="MEETID" precision="22" scale="0"> <comment>外键,和会议管理主键ID对应</comment> </column> </key> <one-to-many class="com.boxun.crm.dao.entities.Tbmeetfile" /> </set> <set name="tbmeetpersons" inverse="true"> <key> <column name="MEETID" precision="22" scale="0"> <comment>外键,和会议管理主键ID对应</comment> </column> </key> <one-to-many class="com.boxun.crm.dao.entities.Tbmeetperson" /> </set>
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。 下载地址:http://json-lib.sourceforge.net/ 还要须要的第3方包: org.apache.commons(3.2以上版本) org.apache.oro net.sf.ezmorph(ezmorph-1.0.4.jar) nu.xom 1、Listboolean[] boolArray = new boolean[]{true,false,true}; JSONArray jsonArray1 = JSONArray.fromObject( boolArray ); System.out.println( jsonArray1 ); // prints [true,false,true] List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list ); System.out.println( jsonArray2 ); / prints ["first","second"] JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" ); System.out.println( jsonArray3 ); // prints ["json","is","easy"] boolean[] boolArray = new boolean[]{true,false,true}; JSONArray jsonArray1 = JSONArray.fromObject( boolArray ); System.out.println( jsonArray1 ); // prints [true,false,true] List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list ); System.out.println( jsonArray2 ); // prints ["first","second"] JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" ); System.out.println( jsonArray3 ); // prints ["json","is","easy"]