python list转换成树形结构_c# List列表数据转换成树形结构

///

///构造树形Json///

public static classTreeJson

{///

///转换树Json///

/// 数据源

/// 父节点

///

public static string ZTreeJson(this List list, string parentId = null) //GetDepartmentTree等方法会给parentId赋值的是null,所以这里的parentId不能写""

{

StringBuilder strJson= newStringBuilder();

List item = list.FindAll(t => t.parentId ==parentId);

strJson.Append("[");if (item.Count > 0)

{foreach (TreeEntity entity initem)

{

strJson.Append("{");

strJson.Append("\"id\":\"" + entity.id + "\",");if (!string.IsNullOrEmpty(entity.text))

{

strJson.Append("\"name\":\"" + entity.text.Replace(" ", "") + "\",");

}if (!string.IsNullOrEmpty(entity.Attribute))

{

strJson.Append("\"" + entity.Attribute + "\":\"" + entity.AttributeValue + "\",");

}if (!string.IsNullOrEmpty(entity.AttributeA))

{

strJson.Append("\"" + entity.AttributeA + "\":\"" + entity.AttributeValueA + "\",");

}if (!string.IsNullOrEmpty(entity.AttributeB))

{

strJson.Append("\"" + entity.AttributeB + "\":\"" + entity.AttributeValueB + "\",");

}if (!string.IsNullOrEmpty(entity.AttributeC))

{

strJson.Append("\"" + entity.AttributeC + "\":\"" + entity.AttributeValueC + "\",");

}if (entity.checkstate != null)

{

strJson.Append("\"checked\":" + entity.checkstate + ",");

}

strJson.Append("\"showcheck\":" + entity.showcheck.ToString().ToLower() + ",");

strJson.Append("\"open\":" + entity.isexpand.ToString().ToLower() + ",");

strJson.Append("\"isParent\":" + entity.hasChildren.ToString().ToLower() + ",");

strJson.Append("\"children\":" + ZTreeJson(list, entity.id) + "");

strJson.Append("},");

}

strJson= strJson.Remove(strJson.Length - 1, 1);

}

strJson.Append("]");returnstrJson.ToString();

}///

///转换树Json///

/// 数据源

/// 父节点

///

public static string TreeToJson(this List list, string ParentId = null) //GetDepartmentTree等方法会给parentId赋值的是null,所以这里的parentId不能写""

{

StringBuilder strJson= newStringBuilder();

List item = list.FindAll(t => t.parentId ==ParentId);

strJson.Append("[");if (item.Count > 0)

{foreach (TreeEntity entity initem)

{

strJson.Append("{");

strJson.Append("\"id\":\"" + entity.id + "\",");if (!string.IsNullOrEmpty(entity.text))

{

strJson.Append("\"text\":\"" + entity.text.Replace(" ", "") + "\",");

}if (!string.IsNullOrEmpty(entity.value))

{

strJson.Append("\"value\":\"" + entity.value + "\",");

}if (!string.IsNullOrEmpty(entity.Attribute))

{

strJson.Append("\"" + entity.Attribute + "\":\"" + entity.AttributeValue + "\",");

}if (!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 + "\",");//}

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("]");returnstrJson.ToString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值