【发布时间】:2016-10-03 07:26:50
【问题描述】:
我正在尝试将从数据库中获取的 DataTable 转换为 Json 格式。但我收到一个错误。
public string ConvertTableToJSON(DataTable objDataTable)
{
ArrayList columnNames = new ArrayList();
int rowCount = objDataTable.Rows.Count;
int currentRow = 1;
string json = "";
//fetching column names
foreach (DataColumn objColumn in objDataTable.Columns)
{
columnNames.Add(objColumn.ColumnName);
}
//generating json string for each row
foreach (DataRow objRow in objDataTable.Rows)
{
json = json + "{";
json = json + ConvertRowToJSON(objRow, columnNames);
json = json + "}";
if (currentRow != rowCount)
{
json = json + ",";
}
currentRow = currentRow + 1;
}
return json;
}
以上是DataTable转Json格式的代码
" Index was outside the bounds of the array." 是调试代码时的错误。这个错误发生在行
if (data[0] == '[' || data[0] == '{')
【问题讨论】: