1、先看效果
2、需要使用层级json格式,如:
3、先不要着急怎么去实现它,先来想想怎么用对象来描述它
4、代码
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { JavaScriptSerializer jss = new JavaScriptSerializer(); List<JsonClass> list = LinqJsonTree(0); list.Insert(0, new JsonClass() { id = -1, children = null, CheckedInfo = true, pid = -1, text = "--请选择--" }); string jsonTree = jss.Serialize(list); string action = Request["action"] ?? ""; if (action == "load") { Response.Write(jsonTree.Replace("CheckedInfo", "checked")); Response.End(); } } } //递归 public List<JsonClass> LinqJsonTree(int parentId) { List<RightInfo> classList = new RightInfoMan().GetMenus(parentId); List<JsonClass> jsonData = new List<JsonClass>(); classList.ForEach(item => { jsonData.Add(new JsonClass { id = item.Id, CheckedInfo = (item.Id == 11), children = LinqJsonTree(item.Id), pid = item.ParentId, text = item.MenuName }); }); return jsonData; }
总结:有时候用别样的方法去实现一个问题 会有趣很多