Home > Web Front-end > JS Tutorial > body text

json-lib appears There is a cycle in the hierarchy solution_json

WBOY
Release: 2016-05-16 18:34:00
Original
1094 people have browsed it

Solution

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

Java code

Copy code The 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[]{//Just set this array and 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[]{//Just set this array and 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 JSON-LIB to let it handle the cycle by itself, which saves trouble. However, if the data is too complex, it will cause data overflow or low efficiency.
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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!