using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Text;
|
|
namespace Terra.YaoGan.Common
|
{
|
public static class DataTableToJson
|
{
|
|
public static string DtToJson(DataTable dt)
|
{
|
if (dt.Rows.Count == 0)
|
{
|
return "";
|
}else
|
{
|
StringBuilder jsonBuilder = new StringBuilder();
|
|
jsonBuilder.Append("[");
|
for (int i = 0; i < dt.Rows.Count; i++)
|
{
|
jsonBuilder.Append("{");
|
for (int j = 0; j < dt.Columns.Count; j++)
|
{
|
jsonBuilder.Append("\"");
|
jsonBuilder.Append(dt.Columns[j].ColumnName);
|
jsonBuilder.Append("\":\"");
|
jsonBuilder.Append(dt.Rows[i][j].ToString());
|
jsonBuilder.Append("\",");
|
}
|
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
|
jsonBuilder.Append("},");
|
}
|
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
|
jsonBuilder.Append("]");
|
return jsonBuilder.ToString();
|
}
|
}
|
|
|
|
|
|
}
|
}
|