对象转json关联属性死循环问题

1、页面不需要展示关联数据时

解决:将关联对象属性排除掉

2、页面需要展示关联数据时

解决:将关联对象改为立即加载,并且将关联对象中的属性排除

下面是去除属性的方法两个

/**
	 * 将指定Java对象转为json,并响应到客户端页面
	 * @param o
	 * @param exclueds
	 */
	public void java2Json(Object o ,String[] exclueds){
		JsonConfig jsonConfig = new JsonConfig();
		//指定哪些属性不需要转json
		jsonConfig.setExcludes(exclueds);
		String json = JSONObject.fromObject(o,jsonConfig).toString();
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 将指定Java对象转为json,并响应到客户端页面
	 * @param o
	 * @param exclueds
	 */
	public void java2Json(List o ,String[] exclueds){
		JsonConfig jsonConfig = new JsonConfig();
		//指定哪些属性不需要转json
		jsonConfig.setExcludes(exclueds);
		String json = JSONArray.fromObject(o,jsonConfig).toString();
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_40280200/article/details/80431270