从数据表 c# 创建复杂的 json
Create complex json from datatable c#
我的数据集中有 3 个数据表。 Table A 与 B 和 C 具有一对多关系。我想在 C# 中使用 Linq 创建如下所示的 Json。有人可以帮帮我吗?我想提前感谢所有指导我或为我的问题提供意见的人。
{
"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"
}
]
}
]
}
首先,您必须创建这些 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; }
}
之后,将值存储在 Result class 的结果对象中,并使用 NewtonSoft 序列化程序对其进行序列化,如下所述:
Result result;
JsonConvert.SerializeObject(result);
您可以通过不创建任何 类 并使用 "dynamic" 类型来简化它。 C# 4 允许您动态创建类型并像使用任何已声明的类型或 类.
一样使用它们
有关动态类型的更多信息:http://www.codeproject.com/Articles/69407/The-Dynamic-Keyword-in-C
您需要做的第一件事是创建动态类型并填充其属性,例如您需要的 Json。请参阅此 post 以了解 Json 的结构和动态关联:http://www.codeproject.com/Tips/834005/storing-GeoJson-coordinates.
有很多方法可以使您对 DataSet 的迭代根据您的数据和列进行动态调整。您可能需要进一步的帮助:Convert datatable to JSON in C#.
希望这对您有所帮助。
我的数据集中有 3 个数据表。 Table A 与 B 和 C 具有一对多关系。我想在 C# 中使用 Linq 创建如下所示的 Json。有人可以帮帮我吗?我想提前感谢所有指导我或为我的问题提供意见的人。
{
"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"
}
]
}
]
}
首先,您必须创建这些 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; }
}
之后,将值存储在 Result class 的结果对象中,并使用 NewtonSoft 序列化程序对其进行序列化,如下所述:
Result result;
JsonConvert.SerializeObject(result);
您可以通过不创建任何 类 并使用 "dynamic" 类型来简化它。 C# 4 允许您动态创建类型并像使用任何已声明的类型或 类.
一样使用它们有关动态类型的更多信息:http://www.codeproject.com/Articles/69407/The-Dynamic-Keyword-in-C
您需要做的第一件事是创建动态类型并填充其属性,例如您需要的 Json。请参阅此 post 以了解 Json 的结构和动态关联:http://www.codeproject.com/Tips/834005/storing-GeoJson-coordinates.
有很多方法可以使您对 DataSet 的迭代根据您的数据和列进行动态调整。您可能需要进一步的帮助:Convert datatable to JSON in C#.
希望这对您有所帮助。