如何从动态数据构造JSON?

时间:2013-07-26 18:51:16

标签: c# json

我有两个系列:

第一个是“父”节点:

e.g。

汽车
人们
公司
...

在这些中,有一些数据。

  1. 汽车
    1.1。本田
    1.2。福特

  2. 2.1。哈里森福特 2.2。精神科
    2.3。杰西卡阿尔巴
  3. 公司
    3.1。甲骨文
    3.2。微软
  4. 依旧......

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

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

    这就是我所拥有的,但它并没有真正给我我需要的东西:

    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;
            }
    

3 个答案:

答案 0 :(得分:2)

假设您已经拥有了Collections,您可以使用像Newtonsoft这样的任何序列化程序将其序列化为JSON

using Newtonsoft.Json;

string json = JsonConvert.SerializeObject(yourlist);

答案 1 :(得分:1)

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

// 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; }
}

然后你将它们序列化为:

public void SerializeData()
{
    var aggregate = new DataAggregate();
    // fill the aggregate accordingly -> from your data source (data tables or what have you)

    // this now has the JSON format format you're looking for
    string jsonData = JsonConvert.SerializeObject(aggregate);
}

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

答案 2 :(得分:0)

这个怎么样。

        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);

上面的代码生成下面的字符串...

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

相关问题