using Newtonsoft.Json.Linq;
/// <summary> /// 遞歸生成部門樹 /// </summary> /// <param name="depList">部門列表</param> /// <param name="parentId">用戶部門的父級部門Id</param> /// <returns></returns> private JArray ConvertDepartTree(IList<AspSysDepartmentDto> depList, int parentId) { var jArray = new JArray(from a in depList where a.ParentId == parentId select new JObject( new JProperty("id", a.Id), new JProperty("text", a.Name), new JProperty("children", ConvertDepartTree(depList, a.Id) ))); return jArray; }
JSON.net Linq To Json文檔地址:https://www.newtonsoft.com/json/help/html/CreatingLINQtoJSON.htmhtml