[英]Convert DataTable to JSON
I've reviewed the answers here: 我在这里查看了答案:
But they don't help for my particular use case. 但是对于我的特定用例,它们没有帮助。 I am retrieving a DataTable from a SQL data adapter and want to convert the DataTable into a List (that's easy enough) and then serialize the List into JSON (using JSON.net, that's easy enough). 我正在从SQL数据适配器检索DataTable,并希望将DataTable转换为List(这很容易),然后将List序列化为JSON(使用JSON.net,这很容易)。
The problem is that I seem to have to use List. 问题是我似乎必须使用List。 Ok, fine - so I have this code: 好的,很好-我有以下代码:
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);
}
}
or 要么
DataTable result = GoMagicallyGatherSomeData();
List<DataRow> ret = result.AsEnumerable().ToList();
When I go to serialize the List, it... isn't what I expect. 当我序列化列表时,它不是我所期望的。
What I would like to get back is: 我想回来的是:
[
{
"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 ... }
]
And what I actually get back is: 我真正得到的是:
[
{
"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
}
]
The other challenge is that I need to do this without awareness of the actual type of the data. 另一个挑战是我需要在不知道数据实际类型的情况下执行此操作。
Any insight? 有见识吗? Anyone have a suggestion on the best way to do this? 有人对最佳方法提出建议吗? Could I get away with copying out the 'table' array from the first serialized DataRow, or, could subsequent serialized DataRows actually contain different data than the first? 我可以避免从第一个序列化的DataRow中复制“表”数组,还是随后的序列化DataRows实际上包含与第一个不同的数据?
You could convert the DataTable
into a List<dynamic>
and then convert it to json. 您可以将DataTable
转换为List<dynamic>
,然后将其转换为json。 For sample, to convert to a dynamic list: 例如,转换为动态列表:
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;
}
You also could make an extension method like this post . 你也可以做一个像这样的扩展方法后 。 Then use it and to convert to List<dynamic>
and finally convert the list to json using the JsonConvert
from Newtonsoft.Json (aka Json.Net)
. 然后使用它并转换为List<dynamic>
,最后使用Newtonsoft.Json (aka Json.Net)
的JsonConvert
将列表转换为json。 For sample: 样品:
var list = ConvertToDataTable(dataTable);
var json = JsonConvert.SerializeObject(list);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.