码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET DataSet查询结果转换为JSON格式数据

时间:2015-01-23 13:20:26      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:


欢迎大家进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; } }

 

ASP.NET DataSet查询结果转换为JSON格式数据

标签:

原文地址:http://www.cnblogs.com/lifueng/p/4243857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!