您的位置:首页 > Web前端 > JavaScript

fastJson数组添加元素时禁用循环引用

2017-03-29 17:22 686 查看
当使用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: