using ShenLong.JSON;
/// <summary>
/// 将table中的数据按列名称输出JSON数据
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
private static JsonData ToJsonData(DataTable table, int start = 0, int end = -1, List<string> ignoreColumns = null)
{
JsonData list = "[]";
int columnCount = table.Columns.Count; // 数据表列数
//foreach (DataRow row in table.Rows)
if (end == -1) end = table.Rows.Count;
for (int index = start; index < end && index < table.Rows.Count; index++)
{
DataRow row = table.Rows[index];
JsonData iteamR = "{}";
for (int i = 0; i < columnCount; i++)
{
string colName = table.Columns[i].ColumnName;
string strVal = row[i].ToString();
var colValue = strVal.Equals("") ? "" : row[i]; // null数据处理
if (ignoreColumns != null && ignoreColumns.Contains(colName)) continue;
iteamR.Add(colName, colValue);
}
list.Add(iteamR);
}
return list;
}
DataTable转Json
最新推荐文章于 2024-05-25 07:12:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)