#region 实现js eval功能, 如:200+200+200-200+210 * 210 public static decimal EvalExpression(string strExpression) { Microsoft.JScript.Vsa.VsaEngine engine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine(); if (string.IsNullOrEmpty(strExpression)) return 0 ; else return Convert.ToDecimal(Microsoft.JScript.Eval.JScriptEvaluate(strExpression, engine)); } #endregion
#region 将一个数据表转换成一个JSON字符串,在客户端可以直接转换成二维数组。 /// <summary> /// 将一个数据表转换成一个JSON字符串,在客户端可以直接转换成二维数组。 /// </summary> /// <param name="source">需要转换的表。</param> /// <returns></returns> public static string DataTableToJson(DataTable source) { if (source.Rows.Count == 0) return ""; StringBuilder sb = new StringBuilder("["); foreach (DataRow row in source.Rows) { sb.Append("["); for (int i = 0; i < source.Columns.Count; i++) { sb.Append('"' + row[i].ToString().Replace("\\", "\\\\").Replace("\'", "\\\'").Replace("\t", " ").Replace("\r", " ").Replace("\n", "<br/>").Replace("\"", "'") + "\","); } sb.Remove(sb.Length - 1, 1); sb.Append("],"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); return sb.ToString(); } /// <summary> /// 返回JSON数据到前台 /// </summary> /// <param name="dt">数据表</param> /// <returns>JSON字符串</returns> public static string DataTableToJsonParam(DataTable dt) { StringBuilder JsonString = new StringBuilder(); if (dt != null && dt.Rows.Count > 0) { JsonString.Append("[ "); for (int i = 0; i < dt.Rows.Count; i++) { JsonString.Append("{ "); for (int j = 0; j < dt.Columns.Count; j++) { if (j < dt.Columns.Count - 1) { JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString().Replace("\\", "\\\\").Replace("\'", "\\\'").Replace("\t", " ").Replace("\r", " ").Replace("\n", "<br/>").Replace("\"", "'") + "\","); } else if (j == dt.Columns.Count - 1) { JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString().Replace("\\", "\\\\").Replace("\'", "\\\'").Replace("\t", " ").Replace("\r", " ").Replace("\n", "<br/>").Replace("\"", "'") + "\""); } } /**/ /*end Of String*/ if (i == dt.Rows.Count - 1) { JsonString.Append("} "); } else { JsonString.Append("}, "); } } JsonString.Append("]"); return JsonString.ToString(); } else { return ""; ; } } #endregion