`
lucky_xingxing
  • 浏览: 119034 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

关于使用ajax请求servletList转换Json抛的异常There is a cycle in the hierarchy!

    博客分类:
  • java
阅读更多
    今天在完善自己做的网上书店的项目的时候,在查看书籍评论的时候准备使用ajax来操作(之前是直接页面全部刷新,感觉不爽,刚刚学了ajax,所以觉得ajax来弄),后台是用hibernate来操作数据库的。但是当写完代码测试的时候突然报错了。 如图

然后就百度 谷歌寻找解决方法,发现大多数都是说因为查询到的对象是放在list集合里面的,然后里面的对象是在数据库通过外键关联了的,也就是在对象模型中有多对一或者一对多等的关联关系,
这样查出来的list集合里面有关联的查出来就是一个对象而不是字符串或者数字,导致JSONArray.fromObject()的时候再内部出现死循环。最后证实确实也是这个原因造成的,然后网上大部分的解决方案都是说的是把有关联的对象的属性给去掉,就 /*过滤掉id属性*/
JsonConfig config1 = new JsonConfig();
         /*用数组的方式写出要过滤的东西*/
config1.setExcludes(new String[]{"user"});
JSONObject p1 = JSONObject.fromObject(review,config1);
第一个参数是放入你要转换的对象,list等.第二个参数放入JsonConfig 对象。
   自己想了些许过后发现这样的话如果你要查询的字段包括关联的对象里面的属性值呢?这样给它去除不转换以后你在界面上就取不到了。所以似乎不能满足我的要求。
    最后自己觉得可以在为数据库表写一个javabean,里面不用对象关联,就如使用jdbc的时候的那样,

然后把从数据库里面读出来的数据list遍历后把值全部取出来并一一赋值给新创建的这个javabean对象,在重新 new一个list,用来装改变后的对象。这样最后再用JSONArray.formObject(list),放入新建的list集合,这样转换就可以成功了。如图成功了没报错。

当然这个方法对于我来说已经可以了,毕竟还是新手,主要是问题能解决就ok了,不知道以后在strut里面是否有改进?自己现在刚刚学完javaweb,然后hibernate都是自学的,ajax刚刚学完,所以大神些看到这个的话就不要说是你这个根本不叫解决了。


    
  • 大小: 248.8 KB
  • 大小: 25.1 KB
  • 大小: 18.1 KB
  • 大小: 155.2 KB
1
1
分享到:
评论

相关推荐

    json-lib出现There is a cycle in the hierarchy解决办法

    如果需要解析的数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛出net.sf.json.JSONException: There is a cycle in the hierarchy异常。

    S2S3H3整合以及泛型Dao与Service封装

    并且对Dao和Service进行了封装,内含.jar包,并且解决了一对多双向关联的could not initialize proxy - no Session错误,同时解决了……invoked on net.sf.json.JSONException: There is a cycle in the hierarchy!...

    计算机组成与结构体系英文课件:Chapter 8 – The Memory System - 2

    Memory Hierarchy is a design strategy implemented to bridge the gap between the Central Processing Unit (CPU) and the various types of memory, which have differing speed, capacity, and cost ...

    Android View Hierarchy,解决View Hierarchy不能启动

    解决这个启动 View Hierarchy时的这个bug: hierarchy viewer unable to debug device

    A consistency improving method in the analytic hierarchy process based on directed circuit analysis

    Test of consistency is critical for the AHP methodology. When a pairwise comparison matrix (PCM) fails the consistency test, the decision maker (DM) needs to make revisions. The state of the art ...

    Sql Server Json解析

    sql server 2014 JSON解析到表函数 CREATE FUNCTION [dbo].[parseJSON]( @JSON NVARCHAR(MAX)) RETURNS @hierarchy TABLE ( element_id INT IDENTITY(1, 1) NOT NULL, ...

    Sql Server解析Json

    ### SQL Server 解析 JSON 字符串方法详解 随着 Web 应用和服务的广泛采用,JSON 成为了一种非常流行的轻量级数据交换格式。在 SQL Server 中处理 JSON 数据变得日益重要,尤其是对于那些需要从非结构化数据源提取...

    WebServiceDemo-CXF-Main

    【WebServiceDemo-CXF-Main】是一个使用Apache CXF框架通过主方法(Main)来发布的Web服务示例。在本文中,我们将深入探讨Web服务、Apache CXF框架以及如何通过Java的main方法启动Web服务。 首先,让我们理解Web...

    使用Hierarchy Viewer工具移除不必要的视图

    Hierarchy Viewer工具可以用来查看视图树(View Tree)并分析视图树中各个视图在测量、布局、绘制阶段所消耗的时间。通过该工具提供的信息,开发者可以找出视图树中那些不必要的视图以及性能瓶颈。在这个demo中,...

    The Elements of Computing Systems

    the best way to understand how computers work is to build one from scratch, and this textbook leads students through twelve chapters and projects that gradually build a basic hardware platform and a ...

    SAP BW Hierarchy Extractor

    ### SAP BW Hierarchy Extractor:从SAP R3系统抽取自定义层级至BW系统的架构 #### 业务场景 在很多企业环境中,管理者需要能够访问其下属员工所负责的数据。尽管SAP Business Warehouse (BW)提供了丰富的业务内容...

    FastReport_.NET_Resigned_NetFx4_Bins_2019.3.20.rar

    * updated a polygon object: now the polygon toolbar is displayed not near to the object, but in the main toolbar and in ribbon; polygon modes are changed, new modes allow editing and adding curves > ...

    Hierarchy面板管理插件(Unity)

    在Unity游戏引擎中,"Hierarchy面板管理插件"是用来增强Unity内置Hierarchy面板功能的重要工具,目的是提高场景管理和编辑效率。本压缩包包含了两个这样的插件:Hierarchy PRO和QHierarchy。 Hierarchy PRO是一个...

    微软内部资料-SQL性能优化3

    An intent lock indicates that SQL Server wants to acquire a shared (S) lock or exclusive (X) lock on some of the resources lower down in the hierarchy. For example, a shared intent lock placed at the ...

    Hierarchical_State_Machine 源代码

    The states at the higher level in hierarchy perform the common message handling, while the lower level states inherit the commonality from higher level ones and perform the state specific functions....

Global site tag (gtag.js) - Google Analytics