简体   繁体   English

从数据表C#创建复杂的JSON

[英]Create complex json from datatable c#

I have 3 datatables in my dataset. 我的数据集中有3个数据表。 Table A has one to many relationship with B and C. I want to create Json like below using Linq in c#. 表A与B和C有一对多的关系。我想像下面那样在C#中使用Linq创建Json。 Can someone please help me? 有人可以帮帮我吗? I would like to thanks in advance to everyone who will guide me or provide me input to my question. 我要在此先感谢所有将指导我或为我的问题提供投入的人。

{
    "A": [
        {
            "id": "0001",
            "type": "donut",
            "name": "Cake",
            "ppu": 0.55,
            "B": [
                {
                    "id": "1001",
                    "type": "Regular"
                }
            ],
            "C": [
                {
                    "id": "5001",
                    "type": "None"
                },
                {
                    "id": "5002",
                    "type": "Glazed"
                }
            ]
        }
    ],
    "A": [
        {
            "id": "0002",
            "type": "Cupcake",
            "name": "Cake",
            "ppu": 2.43,
            "B": [
                {
                    "id": "1001",
                    "type": "Regular"
                }
            ],
            "C": [
                {
                    "id": "5001",
                    "type": "None"
                },
                {
                    "id": "5002",
                    "type": "Glazed"
                }
            ]
        }
    ]
}

First of all, you have to create these classes, 首先,您必须创建这些类,

public class Result
{
    public List<TableA> A { get; set; }
}
public class TableA
{
    public TableA()
    {
          B = new List<TableB>();
          C = new List<TableC>();
    }
    public string id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public float ppu { get; set; }
    public virtual List<TableB> B { get; set; }
    public virtual List<TableC> C { get; set; }
}

public class TableB
{
    public string id { get; set; }
    public string type { get; set; }
}
public class TableC
{
    public string id { get; set; }
    public string type { get; set; }
}

After then, store the values in result object of Result class and serialize this using NewtonSoft serializer as mentioned below: 然后,将值存储在Result类的result对象中,并使用NewtonSoft序列化器对此序列化,如下所述:

Result result;
JsonConvert.SerializeObject(result); 

You can make it simple by without creating any classes and utilizing "dynamic" type. 您可以通过不创建任何类并利用“动态”类型来使其变得简单。 C# 4 allows you to create your types dynamically and use them like any declared types or classes. C#4允许您动态创建类型,并像声明的任何类型或类一样使用它们。

For more on dynamic type: http://www.codeproject.com/Articles/69407/The-Dynamic-Keyword-in-C 有关动态类型的更多信息, 请访问http : //www.codeproject.com/Articles/69407/The-Dynamic-Keyword-in-C

First thing you need is to create dynamic type and fill its properties like the Json you require. 您需要做的第一件事是创建动态类型并像需要的Json一样填充其属性。 Please see this post to see how the structure of Json and dynamic correlates: http://www.codeproject.com/Tips/834005/storing-GeoJson-coordinates . 请参阅这篇文章,以了解Json的结构与动态之间的关系: http : //www.codeproject.com/Tips/834005/storing-GeoJson-coordinates

There are many ways to make your iteration on DataSet to be dynamically adjust based on your data and columns. 有多种方法可以使您在DataSet上的迭代根据您的数据和列进行动态调整。 You may take further help from: Convert datatable to JSON in C# . 您可能会从以下方面获得更多帮助: 在C#中将数据表转换为JSON

I hope this might help. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM