将SQL数据转换为JSON C#
上表需要转换为以下JSON格式将SQL数据转换为JSON C#,c#,json,C#,Json,上表需要转换为以下JSON格式 LIST Time Status A 22:05 0 B 22:10 1 C 22:30 1 A 22:40 0 C 22:50 1 B 22:60 1 上面是一个数据表,现在需要格式化JSON,下面的代码没有给我相同的格式 [ { "name": "A", data: [ [22:05,0], [22:40,0] ] }, {
LIST Time Status
A 22:05 0
B 22:10 1
C 22:30 1
A 22:40 0
C 22:50 1
B 22:60 1
上面是一个数据表,现在需要格式化JSON,下面的代码没有给我相同的格式
[
{ "name": "A",
data: [ [22:05,0],
[22:40,0]
]
},
{ "name": "B",
data: [ [22:10,1],
[22:60,1]
]
}
{ "name": "C",
data: [ [22:30,1],
[22:50,1]
]
}
]
列表行=新列表();
字典行=空;
foreach(数据行dr在dt.行中)
{
行=新字典();
foreach(dt.列中的数据列列列)
{
行添加(列名称,dr[col]);
}
行。添加(行);
}
我建议从表中读取数据,并创建一个自定义类来表示需要序列化的数据。您的数据将在如下所示的类中表示:
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
public List<MyType> ParseTable(DataTable dt) {
var myTypes = new List<MyType>();
var dictionary = new Dictionary<string, List<List<string>>>();
foreach(DataRow dr in dt.Rows) {
var name = dr[0];
var time = dr[1];
var status = dr[2];
if(!dictionary.ContainsKey(dr[0]) {
dictionary[name] = new List<List<string>>();
}
dictionary[name].Add(new List<string>{time, status});
}
foreach(var key = dictionary.Keys) {
myTypes.Add(new MyType {Name = key, Data = dictionary[key]});
}
return myTypes;
}
公共类MyType{
公共字符串名称{get;set;}
公共列表数据{get;set;}
}
然后需要一个方法来解析数据表。像这样的东西可能会做到:
public class MyType {
public string Name {get;set;}
public List<List<string>> Data {get;set;}
}
公共列表解析表(DataTable dt){
var myTypes=新列表();
var dictionary=newdictionary();
foreach(数据行dr在dt.行中){
var name=dr[0];
var时间=dr[1];
var状态=dr[2];
if(!dictionary.ContainsKey(dr[0]){
字典[名称]=新列表();
}
字典[name]。添加(新列表{时间,状态});
}
foreach(var key=dictionary.Keys){
添加(新的MyType{Name=key,Data=dictionary[key]});
}
返回myTypes;
}
然后使用JSON序列化程序将对象的实际序列化处理为JSON
看起来是这样的:
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
public List<MyType> ParseTable(DataTable dt) {
var myTypes = new List<MyType>();
var dictionary = new Dictionary<string, List<List<string>>>();
foreach(DataRow dr in dt.Rows) {
var name = dr[0];
var time = dr[1];
var status = dr[2];
if(!dictionary.ContainsKey(dr[0]) {
dictionary[name] = new List<List<string>>();
}
dictionary[name].Add(new List<string>{time, status});
}
foreach(var key = dictionary.Keys) {
myTypes.Add(new MyType {Name = key, Data = dictionary[key]});
}
return myTypes;
}
公共字符串序列化(列出MyType){
返回JsonConvert.SerializeObject(myTypes);
}
请理解,这是徒手且即兴的。因此,它可能不是最优的,可能需要一些调整。但这应该可以让您达到您想要达到的目的。谢谢,我对将上述数据表中的数据提取到此类中感到困惑。当您将对象序列化为JSON时,序列化程序通常会将其转换为字典没有键-值对。因此,如果您想将JSON结果建模为类型/类,您可以选择字典中的键作为属性。然后,如果您有任何需要创建的JSON列表,您只需使用列表来表示这些项。谢谢,您可以提供一个示例。我得到了包含上述数据的DataTable。需要创建吗这个结构,让我看看know@Alfaromeo更新了我的解决方案。我认为这应该会有所帮助。感谢您在整个过程中耐心地解释这项工作。该解决方案非常有效。顺便说一句,第一个值是日期/时间数据类型,第二个是整数。如何进行此更改,因为我看到所有值都是字符串。