C# 如何从动态数据构造JSON?

C# 如何从动态数据构造JSON?,c#,json,C#,Json,我有两个收藏: 第一个是“父”节点: e、 g 汽车 人们 公司 在每一项下都有一些数据 汽车 1.1. 本田 1.2. 福特 人们 2.1. 哈里森·福特 2.2. Psy 2.3. 杰西卡·阿尔巴 公司 3.1. 甲骨文 3.2. 微软 等等 这些是我可以在C#应用程序中迭代的项目 我将如何构建一个dataTable(或任何最适合它的对象),以生成一个模仿上述树的JSON 这是我所拥有的,但它并不能真正满足我的需要: public DataTable getNotificationType

我有两个收藏:

第一个是“父”节点:

e、 g

汽车
人们
公司

在每一项下都有一些数据

  • 汽车
    1.1. 本田
    1.2. 福特
  • 人们
    2.1. 哈里森·福特
    2.2. Psy
    2.3. 杰西卡·阿尔巴
  • 公司
    3.1. 甲骨文
    3.2. 微软
  • 等等

    这些是我可以在C#应用程序中迭代的项目

    我将如何构建一个dataTable(或任何最适合它的对象),以生成一个模仿上述树的JSON

    这是我所拥有的,但它并不能真正满足我的需要:

    public DataTable getNotificationTypeByUser(int id)
            {
                var bo = new HomeBO();
                var list = bo.GetNotificationsForUser(id);
                var notificationTreeNode = (from GBLNotifications n in list
                                     where n.NotificationCount != 0
                                     select new NotificationTreeNode(n)).ToList();
    
                var table = new DataTable();
    
                var column1 = new DataColumn
                {
                    DataType = Type.GetType("System.String"),
                    ColumnName = "NotificationType"
                };
    
                table.Columns.Add(column1);
    
                var column2 = new DataColumn
                {
                    DataType = Type.GetType("System.String"),
                    ColumnName = "NotificationDescription"
                };
    
                table.Columns.Add(column2);
    
                foreach (var node in notificationTreeNode)
                {
                    var row1 = table.NewRow();
                    row1["NotificationType"] = node.ToString();
                    table.Rows.Add(row1);
    
                    var notifications = bo.GetNotificationsForUser(id, node.NotificationNode.NotificationTypeId);
    
                    foreach (GBLNotifications n in notifications)
                    {
                        var row = table.NewRow();
                        row["NotificationDescription"] = n.NotificationDescription;
                        table.Rows.Add(row);
                    } 
                }
                return table;
            }
    

    假设您已经有了集合,您可以使用Newtonsoft之类的任何序列化程序将其序列化为JSON


    假设您已经有了集合,您可以使用Newtonsoft之类的任何序列化程序将其序列化为JSON


    嗯,不确定代码中发生了什么,但根据层次结构描述判断,您需要这样的对象:

    // along with their properties
    public class Car { }
    public class Person { }
    public class Company { }
    
    public class DataAggregate
    {
        public List<Car> Cars { get; set; }
        public List<Person> People { get; set; }
        public List<Company> Companies { get; set; }
    }
    

    我真的希望我没有误解您的问题。

    嗯,不确定代码中发生了什么,但根据层次结构描述判断,您需要这样的对象:

    // along with their properties
    public class Car { }
    public class Person { }
    public class Company { }
    
    public class DataAggregate
    {
        public List<Car> Cars { get; set; }
        public List<Person> People { get; set; }
        public List<Company> Companies { get; set; }
    }
    
    我真的希望我没有误解你的问题。

    这个怎么样

            var Cars = new[] { "Honda", "Ford"};
            var People = new[] { "Harrison Ford", "Psy", "Jessica Alba" };
            var Companies = new[] { "Oracle", "Microsoft" };
            var result =  new {Cars, People, Companies };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
    
    上面的代码生成下面的字符串

    { “汽车”:[ “本田”, “福特” ], “人”:[ “哈里森·福特”, “精神病”, “杰西卡·阿尔巴” ], “公司”:[ “甲骨文”, “微软” ] }这个怎么样

            var Cars = new[] { "Honda", "Ford"};
            var People = new[] { "Harrison Ford", "Psy", "Jessica Alba" };
            var Companies = new[] { "Oracle", "Microsoft" };
            var result =  new {Cars, People, Companies };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
    
    上面的代码生成下面的字符串

    { “汽车”:[ “本田”, “福特” ], “人”:[ “哈里森·福特”, “精神病”, “杰西卡·阿尔巴” ], “公司”:[ “甲骨文”, “微软” ]
    }

    就是这样,我还没有收藏,因为我不知道如何正确地构建它。你的第一行说我有收藏。无论如何,如果您没有创建具有适当字段的必需类,而不是将它们添加到datatable,那么您可以将它们添加到对象列表中。就是这样,我还没有集合,因为我不知道如何正确构造它。您的第一行说我有集合。不管怎样,如果您不需要,只需使用适当的字段创建所需的类,而不是添加到datatable,您可以将它们添加到对象的列表中