要把前端传来的JSON字符串,取其中的值,节点是无限多,要转为LIST集合才可以方便操作
转为树对象和类对象,方法是一样的
前端JSON字符串传到后端大概如下:
[
{
"title": "一级1",
"id": 1,
"field": "name1",
"checked": true,
"spread": true,
"children": [
{
"title": "二级1-1 可允许跳转",
"id": 3,
"field": "name11",
"href": "/",
"children": [
{
"title": "三级1-1-3",
"id": 23,
"field": "",
"children": [
{
"title": "四级1-1-3-1",
"id": 24,
"field": "",
"children": [
{
"title": "五级1-1-3-1-1",
"id": 30,
"field": ""
},
{
"title": "五级1-1-3-1-2",
"id": 31,
"field": ""
}
]
}
]
},
{
"title": "三级1-1-1",
"id": 7,
"field": "",
"children": [
{
"title": "四级1-1-1-1 可允许跳转",
"id": 15,
"field": "",
"href": "/docs/"
}
]
},
{
"title": "三级1-1-2",
"id": 8,
"field": "",
"children": [
{
"title": "四级1-1-2-1",
"id": 32,
"field": ""
}
]
}
]
},
{
"title": "二级1-2",
"id": 4,
"spread": true,
"children": [
{
"title": "三级1-2-1",
"id": 9,
"field": "",
"disabled": true
},
{
"title": "三级1-2-2",
"id": 10,
"field": ""
}
]
},
{
"title": "二级1-3",
"id": 20,
"field": "",
"children": [
{
"title": "三级1-3-1",
"id": 21,
"field": ""
},
{
"title": "三级1-3-2",
"id": 22,
"field": ""
}
]
}
]
},
{
"title": "一级2",
"id": 2,
"field": "",
"spread": true,
"children": [
{
"title": "二级2-1",
"id": 5,
"field": "",
"spread": true,
"children": [
{
"title": "三级2-1-1",
"id": 11,
"field": ""
},
{
"title": "三级2-1-2",
"id": 12,
"field": ""
}
]
},
{
"title": "二级2-2",
"id": 6,
"field": "",
"children": [
{
"title": "三级2-2-1",
"id": 13,
"field": ""
},
{
"title": "三级2-2-2",
"id": 14,
"field": "",
"disabled": true
}
]
}
]
},
{
"title": "一级3",
"id": 16,
"field": "",
"children": [
{
"title": "二级3-1",
"id": 17,
"field": "",
"fixed": true,
"children": [
{
"title": "三级3-1-1",
"id": 18,
"field": ""
},
{
"title": "三级3-1-2",
"id": 19,
"field": ""
}
]
},
{
"title": "二级3-2",
"id": 27,
"field": "",
"children": [
{
"title": "三级3-2-1",
"id": 28,
"field": ""
},
{
"title": "三级3-2-2",
"id": 29,
"field": ""
}
]
}
]
}
]
后端代码如下:
strjsonUser = Request.Form["jsonUser"];//取前端传来的树的JSON字符串
if (!string.IsNullOrEmpty(strjsonUser))//取选中的员工ID
{
List<TreeObject> listTreeUser = new List<TreeObject>();
listTreeUser = JsonConvert.DeserializeObject<List<TreeObject>>(strjsonUser);//字符串反序列化为LIST对象集合
GetTreeNode(listTreeUser);//循环取子节点给strUser赋值。strUser赋值后为".1.2.3.4.5.6."这样的格式,再分隔后来操作数据
//strUser=.1.2.3.4.5.6."这样的格式
}
无限循环树的对象方法
private void GetTreeNode(List<TreeObject> listTree)//无限内循环获取树的每级节点
{
foreach (TreeObject Tree in listTree)
{
if (Tree.isMoJi == 1)
{
strUser += Tree.id + ".";
}
else
{
if (Tree.children != null)
{
GetTreeNode(Tree.children);
}
}
}
}
树实体类:
public class TreeObject
{
public int id { get; set; }//
public string title { get; set; }//
public string value { get; set; }//
public int topDeptID { get; set; }//上级ID
public int isMoJi { get; set; }//是否末级
public string field { get; set; }//
public bool spread { get; set; }//
public bool disabled { get; set; }//
public bool showCheckbox { get; set; }//
public List<TreeObject> children = new List<TreeObject>();
public void Addchildren(TreeObject node)
{
this.children.Add(node);//showCheckbox
}
}