1.处理点所有的换行
/// <summary>
/// 去掉换行符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string GetDeleteBR(string strinput){
string p = "\\n|\r\n"; //数据库的的换行是\n
string returnstr = System.Text.RegularExpressions.Regex.Replace(strinput, p, " ");
return returnstr;
}
2.转换成对应的json数据格式的字符串
/// <summary>
/// datatable to json
/// </summary>
/// <param name="count"></param>
/// <param name="page"></param>
/// <param name="dt"></param>
/// <returns></returns>
public string DataTableToJson(int count, int page, DataTable dt){
StringBuilder Json = new StringBuilder();
Json.Append("{");
Json.Append("\"total\":" + count + ",");
Json.Append("\"rows\":[");
if (dt != null){
if (dt.Rows.Count > 0) {
for (int i = 0; i < dt.Rows.Count; i++)
{
Json.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
Json.Append("\"" + dt.Columns[j].ColumnName.ToString() +
"\":\"" + GetDeleteBR (dt.Rows[i][j].ToString())+ "\"");
if (j < dt.Columns.Count - 1)
{
Json.Append(",");
Json.Append("\r\n");
}
}
Json.Append("}");
if (i < dt.Rows.Count - 1)
{
Json.Append(",");
}
}
}
}
Json.Append("],");
Json.Append("\"index\":" + page);
Json.Append("}");
return Json.ToString();
}
return Json.ToString();