Json-lib appears There is a cycle in the hierarchy solution

Source: Internet
Author: User
Tags tojson

Solution

1. Set the JSON-LIB to let it filter out fields that cause loops.

Java code
Copy codeThe Code is as follows:
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 [] {// you only need to set this array to specify which fields to filter.
"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 [] {// you only need to set this array to specify which fields to filter. "Consignee", "contract", "coalInfo", "coalType", "startStation", "balanceMan", "endStation "}); string tempStr = "{\" TotalRecords \ ":" + total. toString () + ", \" Datas \ ":" + JSONSerializer. toJSON (list, config ). toString () + "}"; out. print (tempStr );

2. Set the setCycleDetectionStrategy attribute of the JSON-LIB to allow it to process loops on its own, saving trouble but too complicated data can cause data overflow or inefficiency.
Java code
[Code]
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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.