DtatTable转成Json格式

///<summary>

/// DataTable转成Json

///</summary>

///<param name="dt"></param>

///<returns></returns>

publicstaticstring DataTableToJson(DataTable dt)

{

StringBuilder jsonBuilder = newStringBuilder();

 

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().Replace("\"", "\\\""));

jsonBuilder.Append("\",");

}

jsonBuilder.Remove(jsonBuilder.Length - 1, 1);

jsonBuilder.Append("},");

}

jsonBuilder.Remove(jsonBuilder.Length - 1, 1);

jsonBuilder.Append("]");

return jsonBuilder.ToString();

}

DtatTable转成Json格式,布布扣,bubuko.com

DtatTable转成Json格式

上一篇:Thinkphp学习笔记------- RBAC


下一篇:.NET 轻松实现HTML的绝对过滤之SafeHelper