zoukankan      html  css  js  c++  java
  • Datatable序列化和反序列化

    /// <summary>
            /// DataTable序列化
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string SerializeDataTable(this DataTable dt)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dt.Rows)//每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典
                {
                    Dictionary<string, object> result = new Dictionary<string, object>();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        result.Add(dc.ColumnName, dr[dc].ToString());
                    }
                    list.Add(result);
                }
                return serializer.Serialize(list);//调用Serializer方法
            }
    
            /// <summary>
            /// DataTable反序列化
            /// </summary>
            /// <param name="strJson"></param>
            /// <returns></returns>
            public static DataTable DeserializerTable(this string strJson)
            {
                DataTable dt = new DataTable();
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                ArrayList arralList = serializer.Deserialize<ArrayList>(strJson);//反序列化ArrayList类型
                if (arralList.Count == 0)//反序列化后ArrayList个数不为0
                {
                    return dt;
                }
    
                foreach (Dictionary<string, object> row in arralList)
                {
                    if (dt.Columns.Count == 0)//新建的DataTable中无任何信息,为其添加列名及类型
                    {
                        foreach (string key in row.Keys)
                        {
                            dt.Columns.Add(key, row[key].GetType());//添加dt的列名
                        }
                    }
                    DataRow dr = dt.NewRow();
                    foreach (string key in row.Keys)//讲arrayList中的值添加到DataTable中
                    {
                        dr[key] = row[key];//添加列值
                    }
                    dt.Rows.Add(dr);//添加一行
                }
                return dt;
            }
  • 相关阅读:
    样式
    笔试:输入输出
    kafka hbase MR
    数据库
    Spring
    转:Numpy扩充矩阵维度(np.expand_dims, np.newaxis)和删除维度(np.squeeze)的方法
    PyTorch LSTM,batch_first=True对初始化h0和c0的影响
    矩阵F范数的平方转化为矩阵的迹
    Python自带的random库,numpy的随机库,torch的随机函数
    转:sklearn中的高斯混合模型GMM
  • 原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/14030130.html
Copyright © 2011-2022 走看看