mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-05-09 23:28:02 +08:00
79 lines
3.5 KiB
C#
79 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace OpenAuth.Mvc.WebCtrls.Tree
|
|
{
|
|
/// <summary>
|
|
/// 构造树形Json
|
|
/// </summary>
|
|
public static class TreeJson
|
|
{
|
|
/// <summary>
|
|
/// 转换树Json
|
|
/// </summary>
|
|
/// <param name="list">数据源</param>
|
|
/// <param name="ParentId">父节点</param>
|
|
/// <returns></returns>
|
|
public static string TreeToJson(this List<TreeEntity> list, string ParentId = "0")
|
|
{
|
|
StringBuilder strJson = new StringBuilder();
|
|
List<TreeEntity> item = list.FindAll(t => t.parentId == ParentId);
|
|
strJson.Append("[");
|
|
if (item.Count > 0)
|
|
{
|
|
foreach (TreeEntity entity in item)
|
|
{
|
|
strJson.Append("{");
|
|
strJson.Append("\"id\":\"" + entity.id + "\",");
|
|
if (entity.text != null && !string.IsNullOrEmpty(entity.text))
|
|
{
|
|
strJson.Append("\"text\":\"" + entity.text.Replace(" ", "") + "\",");
|
|
}
|
|
|
|
strJson.Append("\"value\":\"" + entity.value + "\",");
|
|
if (entity.Attribute != null && !string.IsNullOrEmpty(entity.Attribute))
|
|
{
|
|
strJson.Append("\"" + entity.Attribute + "\":\"" + entity.AttributeValue + "\",");
|
|
}
|
|
if (entity.AttributeA != null && !string.IsNullOrEmpty(entity.AttributeA))
|
|
{
|
|
strJson.Append("\"" + entity.AttributeA + "\":\"" + entity.AttributeValueA + "\",");
|
|
}
|
|
if (entity.title != null && !string.IsNullOrEmpty(entity.title.Replace(" ", "")))
|
|
{
|
|
strJson.Append("\"title\":\"" + entity.title.Replace(" ", "") + "\",");
|
|
}
|
|
if (entity.img != null && !string.IsNullOrEmpty(entity.img.Replace(" ", "")))
|
|
{
|
|
strJson.Append("\"img\":\"" + entity.img.Replace(" ", "") + "\",");
|
|
}
|
|
if (entity.checkstate != null)
|
|
{
|
|
strJson.Append("\"checkstate\":" + entity.checkstate + ",");
|
|
}
|
|
if (entity.parentId != null)
|
|
{
|
|
strJson.Append("\"parentnodes\":\"" + entity.parentId + "\",");
|
|
}
|
|
if (entity.Level != null)
|
|
{
|
|
strJson.Append("\"Level\":" + entity.Level + ",");
|
|
}
|
|
strJson.Append("\"showcheck\":" + entity.showcheck.ToString().ToLower() + ",");
|
|
strJson.Append("\"isexpand\":" + entity.isexpand.ToString().ToLower() + ",");
|
|
if (entity.complete == true)
|
|
{
|
|
strJson.Append("\"complete\":" + entity.complete.ToString().ToLower() + ",");
|
|
}
|
|
strJson.Append("\"hasChildren\":" + entity.hasChildren.ToString().ToLower() + ",");
|
|
strJson.Append("\"ChildNodes\":" + TreeToJson(list, entity.id) + "");
|
|
strJson.Append("},");
|
|
}
|
|
strJson = strJson.Remove(strJson.Length - 1, 1);
|
|
}
|
|
strJson.Append("]");
|
|
return strJson.ToString();
|
|
}
|
|
}
|
|
}
|