此问题中的代码生成以下 JSON。
代码应该根据 .Where(a => a.Values != null)
行排除空的“子”键,但它不起作用。
我可以在哪里放置 where 子句,以便 JSON 不包含一堆空的“子”数组?
感谢您的帮助。
[{
"NodeID" : 1,
"NodeText" : "Country",
"Children" : [{
"NodeID" : 3,
"NodeText" : "President",
"Children" : []
}, {
"NodeID" : 4,
"NodeText" : "Population",
"Children" : []
}, {
"NodeID" : 5,
"NodeText" : "State",
"Children" : [{
"NodeID" : 6,
"NodeText" : "Governor",
"Children" : []
}, {
"NodeID" : 7,
"NodeText" : "Population",
"Children" : []
}, {
"NodeID" : 8,
"NodeText" : "County",
"Children" : [{
"NodeID" : 9,
"NodeText" : "Population",
"Children" : []
}
]
}
]
}
]
}, {
"NodeID" : 2,
"NodeText" : "Year",
"Children" : []
}
]
下面是生成上述 JSON 的示例代码:
public class Node
{
public int? ParentNodeID { get; set; }
public int NodeID { get; set; }
public string NodeText { get; set; }
public Node(int? parentNodeID, int nodeID, string nodeText)
{
ParentNodeID = parentNodeID;
NodeID = nodeID;
NodeText = nodeText;
}
}
public List<Dictionary<string, object>> BuildTree(int? parentNodeID = null, List<Node> exampleData = null)
{
// kickstart the recursion with example data
if (exampleData == null)
{
exampleData = new List<Node>();
exampleData.Add(new Node(null, 1, "Country"));
exampleData.Add(new Node(null, 2, "Year"));
exampleData.Add(new Node(1, 3, "President"));
exampleData.Add(new Node(1, 4, "Population"));
exampleData.Add(new Node(1, 5, "State"));
exampleData.Add(new Node(5, 6, "Governor"));
exampleData.Add(new Node(5, 7, "Population"));
exampleData.Add(new Node(5, 8, "County"));
exampleData.Add(new Node(8, 9, "Population"));
}
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
var nodes = exampleData.Where(a => a.ParentNodeID == parentNodeID).ToList();
if (nodes != null)
{
result.AddRange(nodes
.Select(a => new Dictionary<string, object> {
{ "NodeID", a.NodeID},
{ "NodeText", a.NodeText },
{ "Children", BuildTree(a.NodeID, exampleData) }
})
.Where(a => a.Values != null) // this doesn't have any effect
.ToList()
);
}
return result;
}
问题是您总是创建一个包含值“Children”的字典,即使没有子项也是如此。
解决方案是将其放入一个条件中,如果没有 child 则不添加该值,否则无论如何您都会继续添加具有空集合值的 Children Key。
这是我用来完成此任务的代码:肉和土 bean 是 Func<T> getNodeDictionary
现在在递归语句中调用。
public static List<Dictionary<string, object>> BuildTree(int? parentNodeID = null, List<Node> exampleData = null)
{
// kickstart the recursion with example data
if (exampleData == null)
{
exampleData = new List<Node>();
exampleData.Add(new Node(null, 1, "Country"));
exampleData.Add(new Node(null, 2, "Year"));
exampleData.Add(new Node(1, 3, "President"));
exampleData.Add(new Node(1, 4, "Population"));
exampleData.Add(new Node(1, 5, "State"));
exampleData.Add(new Node(5, 6, "Governor"));
exampleData.Add(new Node(5, 7, "Population"));
exampleData.Add(new Node(5, 8, "County"));
exampleData.Add(new Node(8, 9, "Population"));
}
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
var nodes = exampleData.Where(a => a.ParentNodeID == parentNodeID).ToList();
if (nodes != null)
{
Func<Node, Dictionary<string, object>> getNodeDictionary = n => {
var children = BuildTree(n.NodeID, exampleData); // still recursive
var returnDictionary = new Dictionary<string, object> { // these 2 nodes always get added
{ "NodeID", n.NodeID},
{ "NodeText", n.NodeText }
};
// This ensures we only add Children if there are actually any children
if (children.Any())
{
returnDictionary.Add("Children", children);
}
return returnDictionary;
};
// No need for where clause since we now do not add the empty elements
result.AddRange(nodes
.Select(a => getNodeDictionary(a))
.ToList()
);
}
return result;
}
我是一名优秀的程序员,十分优秀!