当使用fastJson时,在JSONArray中add多个json对象,若各json中存在相同元素,则会出现ref{…}表示的循环引用,想要禁止循环引用,则对加入JSONArray中的每个元素进行如下转换,再add到JSONArray即可。
String str = JSON.toJSONString(json,SerializerFeature.DisableCircularReferenceDetect);
JSONObject j = JSON.parseObject(str);
arry.add(j);
/**
* 列表数据(已知应用id)
*
* @param appId
* @param request
* @param response
* @return
*/
public JSONArray tabData2(String appId ) {
DataSourceHolder.setDataSources(Global.getConfig("centerDataSource"));
Service app = serviceService.get(appId);
DataSourceHolder.reSet();
Map<String, String> map = new HashMap<String, String>();
JSONArray arry = new JSONArray();
JSONObject json = new JSONObject();
if (app != null && StringUtils.isNotBlank(app.getId())) {
List<Api> apiList = apiData(app.getId());
for (Api api : apiList) {
if (api != null && StringUtils.isNotBlank(api.getId())) {
List<Apply> applyList = applyData(api.getId());
for (Apply a : applyList) {
map = statisticData(a.getId(), api.getId());
json.put("success", map.get("success"));
json.put("failure", map.get("failure"));
json.put("applyOffice", a.getCreateBy().getCompany().getName());
json.put("applyName", a.getCreateBy().getName());
json.put("apiName", api.getName());
json.put("resTime", map.get("resTime"));
String str = JSON.toJSONString(json,SerializerFeature.DisableCircularReferenceDetect);
JSONObject j = JSON.parseObject(str);
arry.add(j);
System.out.println(arry);
}
}
}
}
return arry;
}