温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

json格式后台转换使用

发布时间:2020-06-20 23:13:07 来源:网络 阅读:484 作者:zyhlwzy 栏目:编程语言

 ashx.cs中需要的工作:

一般处理程序中使用Session:
如果想要在ashx中应用Session则必须先引入一个头文件using System.Web.SessionState;
然后
public class JsonHandler : IHttpHandler, IRequiresSessionState/*IRequiresSessionState是新添加的,需要实现这个接口才能使用Session*/
{
public void Proce***equest(HttpContext context)
{
HttpContext.Current.Response.CacheControl = "no-cache";
string name = context.Session["Name"].ToString();  /*注意这里需要加上context*/
}
 
一般处理程序当中清除浏览器缓存:
HttpContext.Current.Response.CacheControl = "no-cache";   /*意思就是把当前响应中的缓存设置为不缓存,即为清除缓存!一般是加在最前面*/
 
将DataSet类型的数据转化为JSON格式的数据:
public static string ToJson(DataTable dt)  //将DataSet类型的数据转化为JSON格式的数据
{
            StringBuilder jsonBuilder = new StringBuilder();
            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());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");        
            return jsonBuilder.ToString();
}  
 public static string ToJson(DataSet ds)
{
       StringBuilder json = new StringBuilder();
       foreach (DataTable dt in ds.Tables)
       {
                json.Append("{\"");
                json.Append(dt.TableName);
                json.Append("\":");
                json.Append(ToJson(dt));
                json.Append("}");
       }
       return json.ToString();
}  
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI