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

当使用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;
	}

猜你喜欢

转载自blog.csdn.net/anne_it_blog/article/details/68067383