日期:2014-05-16 浏览次数:20429 次
JSONArray.fromObject方法时,出现
net.sf.json.JSONException: There is a cycle in the hierarchy
$(document).ready(function(){ $("#submitRe").click(function(){ $.post("loginAction.action",null,callback) }) callback=function(jsonData,textStats){ if("success"==textStats){ alert(jsonData); } } })
package com.accp.firststruts2.actions; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; import net.sf.json.util.CycleDetectionStrategy; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import com.accp.firststruts2.dto.Users; import com.opensymphony.xwork2.ActionSupport; /** * * @author softceo945 * */ public class LoginAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{ private static final long serialVersionUID = -6884393637128187643L; HttpServletRequest request = null; HttpServletResponse response = null; public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public String execute() throws Exception { List<Map<String,Object>> listAll = new ArrayList<Map<String,Object>>();//最终返回的数据集合 String str ="";//JSON文本 for (int i = 0; i < 10; i++) { Users users = new Users(); users.setUsername("Users "+i); users.setPsd(Math.random()+""); users.setAge(i+15); users.setSex('男'); Map<String,Object> a = new HashMap<String, Object>(); a.put(users.getUsername(), users); listAll.add(a); } str=JSONArray.fromObject(listAll).toString(); //如果存在关联关系映射,即N-M,或M-M时,调用下面的代码来处理,时间关系,本文就没建立N-M,或M-M关系了 // JsonConfig config = new JsonConfig(); // config.setIgnoreDefaultExcludes(false); // config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); // JSONArray JsonArr=JSONArray.fromObject(listAll, config); // str=JsonArr.toString(); response.getWriter().write(str); return null; } }
//如果存在关联关系映射,即N-M,或M-M时,调用下面的代码来处理,时间关系,本文就没建立N-M,或M-M关系了 // JsonConfig config = new JsonConfig(); // config.setIgnoreDefaultExcludes(false); // config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); // JSONArray JsonArr=JSONArray.fromObject(listAll, config); // str=JsonArr.toString();