ASP.NET DataSet查询结果转换为JSON格式数据
欢迎大家进QQ群来讨论:84479667
方法如下: public static string nulldata = "{\"result\":\"true\",\"msg\":\"没有相关数据\"}"; #region 查询结果转为json /// <summary> /// 查询结果转为json /// </summary> /// <param name="dt"></param> /// <param name="count"></param> /// <returns></returns> public static string GetDataToJSON(DataTable dt, int count = 0) { JavaScriptSerializer jss = new JavaScriptSerializer(); System.Collections.ArrayList dic = new System.Collections.ArrayList(); if (dt != null && dt.Rows.Count > 0) { if (count == 0) { count = dt.Rows.Count; } foreach (DataRow dr in dt.Rows) { System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>(); foreach (DataColumn dc in dt.Columns) { //string dcvalue = Common.PageBase.delhtml(dr[dc.ColumnName].ToString()); drow.Add(dc.ColumnName, dr[dc.ColumnName]); } dic.Add(drow); } //bool result = true; string JsonString = jss.Serialize(dic); string p = @"\\/Date\((\d+)\)\\/"; MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString); //MatchEvaluator img_urlEvaluator = new MatchEvaluator(GetImgUrl); Regex reg = new Regex(p); //Regex img_url = new Regex("src=\\\\\"(.*?)\""); //Regex img_url = new Regex("src=\"(.*?)\""); JsonString = reg.Replace(JsonString, matchEvaluator); //JsonString = img_url.Replace(JsonString, img_urlEvaluator); return "{\"result\": \"true\"" + "," + "\"rows\":" + count + "," + "\"msg\":" + JsonString + "}"; } else { return nulldata; } } #endregion /// <summary> /// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串 /// </summary> /// <param name="m"></param> /// <returns></returns> private static string ConvertJsonDateToDateString(Match m) { string result = string.Empty; DateTime dt = new DateTime(1970, 1, 1); dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value)); dt = dt.ToLocalTime(); result = dt.ToString("yyyy-MM-dd HH:mm:ss"); return result; } public string CreateJsonParameters(DataTable dt, int count = 0) { if (dt == null || dt.Rows.Count == 0) { return nulldata; } if (count == 0) { count = dt.Rows.Count; } bool result = true; StringBuilder JsonString = new StringBuilder(); //Exception Handling if (dt != null && dt.Rows.Count > 0) { JsonString.Append("{ "); JsonString.Append("\"result\":" + result + "," + "rows:" + count + "," + "msg:[ "); 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() + "\","); } else if (j == dt.Columns.Count - 1) { JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\""); } } /*end Of String*/ if (i == dt.Rows.Count - 1) { JsonString.Append("} "); } else { JsonString.Append("}, "); } } JsonString.Append("]}"); return JsonString.ToString(); } else { return null; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。