将 DataTable 转换为 JSON
Convert DataTable to JSON
我已经查看了此处的答案:
- How do you convert a DataTable into a generic list?
- Convert DataTable to Generic List in C#
和
- Fastest way to convert datatable to generic list
但它们对我的特定用例没有帮助。我正在从 SQL 数据适配器中检索数据表,并希望将数据表转换为列表(这很容易),然后将列表序列化为 JSON(使用 JSON.net,这很容易).
问题是我好像不得不用List。好的,很好 - 所以我有这个代码:
DataTable result = GoMagicallyGatherSomeData();
List<DataRow> ret = new List<DataRow>();
if (result != null && result.Rows.Count > 0)
{
foreach (DataRow curr in result.Rows)
{
ret.Add(curr);
}
}
或
DataTable result = GoMagicallyGatherSomeData();
List<DataRow> ret = result.AsEnumerable().ToList();
当我去序列化列表时,它...不是我所期望的。
我想回复的是:
[
{
"TestId":1,
"AccountId":1,
"SomeString":"This is an updated test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{
"TestId":3,
"AccountId":1,
"SomeString":"This is a third test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{ ... removed for brevity ... }
]
而我实际得到的是:
[
{
"RowError":"",
"RowState":2,
"Table":[
{
"TestId":1,
"AccountId":1,
"SomeString":"This is an updated test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{
"TestId":3,
"AccountId":1,
"SomeString":"This is a second test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{ ... removed for brevity ... }
],
"ItemArray":[
1,
1,
"This is an updated test",
"2016-01-01T00:00:00Z",
5.55
],
"HasErrors":false
},
{
"RowError":"",
"RowState":2,
"Table":[
... there seems to be an instance of this for every row in the result ...
],
"ItemArray":[
1,
1,
"This is an updated test",
"2016-01-01T00:00:00Z",
5.55
],
"HasErrors":false
}
]
另一个挑战是我需要在不知道数据的实际类型的情况下执行此操作。
有什么见解吗?有人对执行此操作的最佳方法有建议吗?我能否从第一个序列化的 DataRow 中复制出 'table' 数组,或者后续序列化的 DataRows 实际上包含与第一个不同的数据?
您可以将 DataTable
转换为 List<dynamic>
,然后将其转换为 json。例如,要转换为动态列表:
public static List<dynamic> ConvertToDataTable(DataTable dataTable)
{
var result = new List<dynamic>();
foreach (DataRow row in dataTable.Rows)
{
dynamic dyn = new ExpandoObject();
foreach (DataColumn column in dataTable.Columns)
{
var dic = (IDictionary<string, object>)dyn;
dic[column.ColumnName] = row[column];
}
result.Add(dyn);
}
return result;
}
您也可以制作这样的扩展方法post. Then use it and to convert to List<dynamic>
and finally convert the list to json using the JsonConvert
from Newtonsoft.Json (a.k.a. Json.Net)
。例如:
var list = ConvertToDataTable(dataTable);
var json = JsonConvert.SerializeObject(list);
我已经查看了此处的答案:
- How do you convert a DataTable into a generic list?
- Convert DataTable to Generic List in C# 和
- Fastest way to convert datatable to generic list
但它们对我的特定用例没有帮助。我正在从 SQL 数据适配器中检索数据表,并希望将数据表转换为列表(这很容易),然后将列表序列化为 JSON(使用 JSON.net,这很容易).
问题是我好像不得不用List。好的,很好 - 所以我有这个代码:
DataTable result = GoMagicallyGatherSomeData();
List<DataRow> ret = new List<DataRow>();
if (result != null && result.Rows.Count > 0)
{
foreach (DataRow curr in result.Rows)
{
ret.Add(curr);
}
}
或
DataTable result = GoMagicallyGatherSomeData();
List<DataRow> ret = result.AsEnumerable().ToList();
当我去序列化列表时,它...不是我所期望的。
我想回复的是:
[
{
"TestId":1,
"AccountId":1,
"SomeString":"This is an updated test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{
"TestId":3,
"AccountId":1,
"SomeString":"This is a third test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{ ... removed for brevity ... }
]
而我实际得到的是:
[
{
"RowError":"",
"RowState":2,
"Table":[
{
"TestId":1,
"AccountId":1,
"SomeString":"This is an updated test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{
"TestId":3,
"AccountId":1,
"SomeString":"This is a second test",
"SomeTimestamp":"2016-01-01T00:00:00Z",
"SomeDecimal":5.55
},
{ ... removed for brevity ... }
],
"ItemArray":[
1,
1,
"This is an updated test",
"2016-01-01T00:00:00Z",
5.55
],
"HasErrors":false
},
{
"RowError":"",
"RowState":2,
"Table":[
... there seems to be an instance of this for every row in the result ...
],
"ItemArray":[
1,
1,
"This is an updated test",
"2016-01-01T00:00:00Z",
5.55
],
"HasErrors":false
}
]
另一个挑战是我需要在不知道数据的实际类型的情况下执行此操作。
有什么见解吗?有人对执行此操作的最佳方法有建议吗?我能否从第一个序列化的 DataRow 中复制出 'table' 数组,或者后续序列化的 DataRows 实际上包含与第一个不同的数据?
您可以将 DataTable
转换为 List<dynamic>
,然后将其转换为 json。例如,要转换为动态列表:
public static List<dynamic> ConvertToDataTable(DataTable dataTable)
{
var result = new List<dynamic>();
foreach (DataRow row in dataTable.Rows)
{
dynamic dyn = new ExpandoObject();
foreach (DataColumn column in dataTable.Columns)
{
var dic = (IDictionary<string, object>)dyn;
dic[column.ColumnName] = row[column];
}
result.Add(dyn);
}
return result;
}
您也可以制作这样的扩展方法post. Then use it and to convert to List<dynamic>
and finally convert the list to json using the JsonConvert
from Newtonsoft.Json (a.k.a. Json.Net)
。例如:
var list = ConvertToDataTable(dataTable);
var json = JsonConvert.SerializeObject(list);