TreeJson.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using LeaRun.Util.WebControl;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace LeaRun.Util.WebControl
  7. {
  8. /// <summary>
  9. /// 构造树形Json
  10. /// </summary>
  11. public static class TreeJson
  12. {
  13. /// <summary>
  14. /// 转换树Json
  15. /// </summary>
  16. /// <param name="list">数据源</param>
  17. /// <param name="ParentId">父节点</param>
  18. /// <returns></returns>
  19. public static string TreeToJson(this List<TreeEntity> list, string ParentId = "0")
  20. {
  21. StringBuilder strJson = new StringBuilder();
  22. List<TreeEntity> item = list.FindAll(t => t.parentId == ParentId);
  23. strJson.Append("[");
  24. if (item.Count > 0)
  25. {
  26. foreach (TreeEntity entity in item)
  27. {
  28. strJson.Append("{");
  29. strJson.Append("\"id\":\"" + entity.id + "\",");
  30. if (entity.text != null && !string.IsNullOrEmpty(entity.text))
  31. {
  32. strJson.Append("\"text\":\"" + entity.text.Replace("&nbsp;", "") + "\",");
  33. }
  34. if (entity.value != null && !string.IsNullOrEmpty(entity.value))
  35. {
  36. strJson.Append("\"value\":\"" + entity.value + "\",");
  37. }
  38. if (entity.Code != null && !string.IsNullOrEmpty(entity.Code))
  39. {
  40. strJson.Append("\"code\":\"" + entity.Code + "\",");
  41. }
  42. if (entity.Attribute != null && !string.IsNullOrEmpty(entity.Attribute))
  43. {
  44. strJson.Append("\"" + entity.Attribute + "\":\"" + entity.AttributeValue + "\",");
  45. }
  46. if (entity.AttributeA != null && !string.IsNullOrEmpty(entity.AttributeA))
  47. {
  48. strJson.Append("\"" + entity.AttributeA + "\":\"" + entity.AttributeValueA + "\",");
  49. }
  50. if (entity.title != null && !string.IsNullOrEmpty(entity.title.Replace("&nbsp;", "")))
  51. {
  52. strJson.Append("\"title\":\"" + entity.title.Replace("&nbsp;", "") + "\",");
  53. }
  54. if (entity.img != null && !string.IsNullOrEmpty(entity.img.Replace("&nbsp;", "")))
  55. {
  56. strJson.Append("\"img\":\"" + entity.img.Replace("&nbsp;", "") + "\",");
  57. }
  58. if (entity.checkstate != null)
  59. {
  60. strJson.Append("\"checkstate\":" + entity.checkstate + ",");
  61. }
  62. if (entity.parentId != null)
  63. {
  64. strJson.Append("\"parentnodes\":\"" + entity.parentId + "\",");
  65. }
  66. if (entity.Level != null)
  67. {
  68. strJson.Append("\"Level\":" + entity.Level + ",");
  69. }
  70. strJson.Append("\"showcheck\":" + entity.showcheck.ToString().ToLower() + ",");
  71. strJson.Append("\"isexpand\":" + entity.isexpand.ToString().ToLower() + ",");
  72. if (entity.complete == true)
  73. {
  74. strJson.Append("\"complete\":" + entity.complete.ToString().ToLower() + ",");
  75. }
  76. strJson.Append("\"hasChildren\":" + entity.hasChildren.ToString().ToLower() + ",");
  77. strJson.Append("\"ChildNodes\":" + TreeToJson(list, entity.id) + "");
  78. strJson.Append("},");
  79. }
  80. strJson = strJson.Remove(strJson.Length - 1, 1);
  81. }
  82. strJson.Append("]");
  83. return strJson.ToString();
  84. }
  85. }
  86. }