`
liutao841204
  • 浏览: 13640 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

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


1.设置JSON-LIB让其过滤掉引起循环的字段
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
config.setExcludes(new String[]{//只要设置这个数组,指定过滤哪些字段。
"consignee",
"contract",
"coalInfo",
"coalType",
"startStation",
"balanceMan",
"endStation"
});
String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";
out.print(tempStr);
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
config.setExcludes(new String[]{//只要设置这个数组,指定过滤哪些字段。 "consignee", "contract", "coalInfo", "coalType", "startStation", "balanceMan", "endStation" }); String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}"; out.print(tempStr);

2.设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环,省事但是数据过于复杂的话会引起数据溢出或者效率低下。
Java代码

JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd")); //date processor register
String tempStr = "{\"TotalRecords\":"+ total.toString() +",\"Datas\":"+JSONSerializer.toJSON(list,config).toString()+"}";
out.print(tempStr);


原地址:http://hi.baidu.com/kfo2046/blog/item/f384c5faf9276dd3b58f3172.html
分享到:
评论

相关推荐

    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

    BIG文件编辑器

    The Lord of the Rings(tm), The Battle for Middle-earth(tm) is a trademark or registered trademark of Electronic Arts in the USA and/or other countries. All rights reserved. LucasArts, the LucasArts ...

    win 3.11 for workgroup tcpip支持

    conditions is to put a #PRE against the name in the LMHOSTS file. This causes the name to be stored in the name cache when the machine is first initialized, so the name gets resolved without the ...

    Git-2.21.0-64-bit.zip

    value for the HEAD symbolic ref when the namespace feature is in use, which has been corrected. * "git am -i --resolved" segfaulted after trying to see a commit as if it were a tree, which has ...

    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 > ...

    前端项目-d3-hierarchy.zip

    在“d3-hierarchy-master”这个项目中,重点在于层次数据(Hierarchy Data)的可视化。层次数据通常表现为树形结构,例如组织架构、文件系统或者生物学的物种分类等。D3提供了hierarchy模块,专门处理这类数据,可以...

    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 ...

    CScrollHelper CScrollWnd

    Implementing standard scrolling behavior for a custom CWnd or CDialog-derived class using MFC is fairly non... No change in your inheritance hierarchy is required to use the helper class (CScrollHelper).

    Sql Server解析Json

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

    BSD4.4设计手册.7z

    The filesystem is a set of named files, organized in a tree-structured hierarchy of directories, and of operations to manipulate them, as presented in Chapter 7. Files reside on physical media such ...

    S7-400 CPU Specifications Manual .pdf

    The manual is a comprehensive guide for engineers, technicians, and maintenance personnel involved in the planning, commissioning, and maintenance of S7-400 systems. It is essential reading for ...

    Pro-Deep-Learning-with-TensorFlow.pdf

    Deep learning is a branch of machine learning where you model the world in terms of a hierarchy of concepts. This pattern of learning is similar to the way a human brain learns, and it allows ...

    QHierarchy 4.2 最新版Unity插件

    - Displaying the list of GameObjects in the form of a tree - Option to add right indent (useful if you use other plugins that add another icons to the hierarchy window) - Any feature can be disabled ...

    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 ...

Global site tag (gtag.js) - Google Analytics