Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在维护父子关系的同时将datatable转换为JSON树_C#_.net_Tree_Json.net - Fatal编程技术网

C# 在维护父子关系的同时将datatable转换为JSON树

C# 在维护父子关系的同时将datatable转换为JSON树,c#,.net,tree,json.net,C#,.net,Tree,Json.net,我有一个包含树结构的数据表。我想将其转换为JSON对象,并将其绑定到ExtJS树。我能够将DataTable转换为树结构(感谢Stackoverflow用户之一),但无法将其转换为JSON。我想返回JSON对象,我应该能够将其绑定到EXTJs树 **我不想这样做:string json=JsonConvert.serialized对象(table,Formatting.Indented) 这只会将DataTable转换为JSON,但不会保留父子关系** 这是我的密码 public class

我有一个包含树结构的数据表。我想将其转换为JSON对象,并将其绑定到ExtJS树。我能够将DataTable转换为树结构(感谢Stackoverflow用户之一),但无法将其转换为JSON。我想返回JSON对象,我应该能够将其绑定到EXTJs树

**我不想这样做:string json=JsonConvert.serialized对象(table,Formatting.Indented)

这只会将DataTable转换为JSON,但不会保留父子关系**

这是我的密码

 public class Node<T>
    {
        public T Item { get; internal set; }
        public int Level { get; internal set; }
        public Node<T> Parent { get; internal set; }
        public IList<Node<T>> Children { get; internal set; }

    }


 public static class Program
    {
        private static void Main(string[] args)
        {
            DataTable table = new DataTable();
            table.Columns.Add("name", typeof(string));
            table.Columns.Add("key", typeof (string));
            table.Columns.Add("parentkey", typeof (string));
            table.Columns.Add("Level", typeof (int));


            table.Rows.Add("A", "A1", null, 1);
            table.Rows.Add("B", "A2", "A1", 2);
            table.Rows.Add("C", "A3", "A1", 2);
            table.Rows.Add("D", "A4", "A1", 2);

            table.Rows.Add("E", "A5", "A2", 3);
            table.Rows.Add("F", "A6", "A5", 4);
            table.Rows.Add("G", "A7", "A3", 3);
            table.Rows.Add("H", "A8", "A4", 3);


            table.Rows.Add("I", "A9", "A4", 3);
            table.Rows.Add("J", "A10", "A4", 3);
            table.Rows.Add("K", "A11", "A10", 4);
            table.Rows.Add("L", "A12", "A10", 4);

            table.Rows.Add("M", "A13", "A12", 5);
            table.Rows.Add("N", "A14", "A12", 5);
            table.Rows.Add("O", "A15", "A10", 4);

            table.Rows.Add("P", "A16", null, 1);
            table.Rows.Add("Q", "A17", "A16", 2);

            DataView view = table.DefaultView;

            // By default, the first column sorted ascending.
            view.Sort = "Level, ParentKey DESC";


            var hierarchy =
                table.AsEnumerable()
                    .ToHierarchy(row => row.IsNull("parentkey"),
                                 (parent, child) => parent.Field<string>("key") ==
                                                    child.Field<string>("parentkey"));


           //HOW TO CONVERT THIS HIERARCHY INTO JSON???

            Console.ReadKey();

        }

        public static IEnumerable<Node<T>> ToHierarchy<T>(this IEnumerable<T> source,Func<T, bool> startWith, Func<T, T, bool> connectBy)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (startWith == null) throw new ArgumentNullException("startWith");
            if (connectBy == null) throw new ArgumentNullException("connectBy");
            return source.ToHierarchy(startWith, connectBy, null);
        }

        private static IEnumerable<Node<T>> ToHierarchy<T>(this IEnumerable<T> source,Func<T, bool> startWith,Func<T, T, bool> connectBy,Node<T> parent)
        {
            int level = (parent == null ? 0 : parent.Level + 1);

            var roots = from item in source
                        where startWith(item)
                        select item;
            foreach (T value in roots)
            {
                var children = new List<Node<T>>();
                var newNode = new Node<T>
                {
                    Level = level,
                    Parent = parent,
                    Item = value,
                    Children = children.AsReadOnly()
                };

                T tmpValue = value;
                children.AddRange(source.ToHierarchy(possibleSub => connectBy(tmpValue, possibleSub), connectBy, newNode));

                yield return newNode;
            }
        }
    }
公共类节点
{
公共T项{get;内部集合;}
公共整数级别{get;内部集合;}
公共节点父节点{get;内部集;}
公共IList子项{get;内部集;}
}
公共静态类程序
{
私有静态void Main(字符串[]args)
{
DataTable=新的DataTable();
表.列.添加(“名称”,类型(字符串));
表.列.添加(“键”,类型(字符串));
table.Columns.Add(“parentkey”,typeof(string));
表.列.添加(“级别”,类型(int));
表.行.添加(“A”,“A1”,null,1);
表.行.添加(“B”、“A2”、“A1”和“2”);
表.行.添加(“C”、“A3”、“A1”和“2”);
表.行.添加(“D”、“A4”、“A1”和“2”);
表.行.添加(“E”、“A5”、“A2”和“3”);
表.行.添加(“F”、“A6”、“A5”、4);
表.行.添加(“G”、“A7”、“A3”和“3”);
表.行.添加(“H”、“A8”、“A4”和“3”);
表.行.加上(“I”、“A9”、“A4”和“3”);
表.行.添加(“J”、“A10”、“A4”和“3”);
表。行。添加(“K”、“A11”、“A10”、4);
表。行。添加(“L”、“A12”、“A10”、4);
表。行。添加(“M”、“A13”、“A12”、5);
表。行。添加(“N”、“A14”、“A12”、5);
表。行。添加(“O”、“A15”、“A10”、4);
表.行.添加(“P”,“A16”,空,1);
表。行。添加(“Q”、“A17”、“A16”、2);
DataView视图=table.DefaultView;
//默认情况下,第一列按升序排序。
view.Sort=“Level,ParentKey DESC”;
风险等级=
表1.AsEnumerable()
.ToHierarchy(row=>row.IsNull(“parentkey”),
(父,子)=>父字段(“键”)==
child.Field(“parentkey”);
//如何将此层次结构转换为JSON???
Console.ReadKey();
}
公共静态IEnumerable ToHierarchy(此IEnumerable源、Func startWith、Func connectBy)
{
如果(source==null)抛出新的ArgumentNullException(“source”);
如果(startWith==null)抛出新的ArgumentNullException(“startWith”);
如果(connectBy==null)抛出新的ArgumentNullException(“connectBy”);
返回source.ToHierarchy(startWith、connectBy、null);
}
私有静态IEnumerable ToHierarchy(此IEnumerable源、Func startWith、Func connectBy、节点父节点)
{
int-level=(parent==null?0:parent.level+1);
变量根=来自源中的项
其中startWith(项目)
选择项目;
foreach(根中的T值)
{
var children=新列表();
var newNode=新节点
{
级别=级别,
父=父,
项目=价值,
Children=Children.AsReadOnly()
};
T tmpValue=值;
AddRange(source.ToHierarchy(possibleSub=>connectBy(tmpValue,possibleSub),connectBy,newNode));
产生返回新节点;
}
}
}

可能重复@Bob。我希望在将对象转换为JSON时保持父子关系。使用字符串json=JsonConvert.SerializedObject(表,格式化.缩进);只需将每一行转换为JSON,而不考虑父子关系。
JsonConvert.SerializeObject(层次结构)有什么问题?您在
var层次结构
对象上尝试过吗?@Bob。嗯,层次结构对象具有父子关系,但不确定如何转换它。我仍在使用该对象&希望我能够转换它。