• dataset datatable 转json


        class ToJosn
        {
            #region dataTable转换成Json格式
            /// <summary>      
            /// dataTable转换成Json格式      
            /// </summary>      
            /// <param name="dt"></param>      
            /// <returns></returns>      
            public static string ToJson(ArrayList aliJson)
            {
                StringBuilder sbuBuilder = new StringBuilder();
                sbuBuilder.Append("{"Value":[");
                for (int i = 0; i < aliJson.Count; i++)
                {
                    sbuBuilder.Append(""");
                    sbuBuilder.Append(aliJson[i].ToString().Replace(""", "\""));
                    sbuBuilder.Append("",");
                }
                if (aliJson.Count > 0)
                {
                    sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                }
                sbuBuilder.Append("]}");
                string strJson = sbuBuilder.ToString();
                strJson = strJson.Replace("
    ", "<br />");
                strJson = strJson.Replace("
    ", "<br />");
                return strJson;//sbuBuilder.ToString();
            }
            public static string ToJson(DataTable dtaJson)
            {
                StringBuilder sbuBuilder = new StringBuilder();
                sbuBuilder.Append("{"Rows":[");
                for (int i = 0; i < dtaJson.Rows.Count; i++)
                {
                    sbuBuilder.Append("[");
                    for (int j = 0; j < dtaJson.Columns.Count; j++)
                    {
                        sbuBuilder.Append(""");
                        sbuBuilder.Append(dtaJson.Rows[i][j].ToString().Replace(""", "\"").Replace("
    ", "<br>"));
                        sbuBuilder.Append("",");
                    }
                    sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                    sbuBuilder.Append("],");
                }
                if (dtaJson.Rows.Count > 0)
                {
                    sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                }
                sbuBuilder.Append("]}");
    
                string strJson = sbuBuilder.ToString();
                strJson = strJson.Replace("
    ", "<br />");
                strJson = strJson.Replace("
    ", "<br />");
    
                return strJson;//sbuBuilder.ToString();
            }
    
            #endregion dataTable转换成Json格式
    
            #region DataSet转换成Json格式
            /// <summary>      
            /// DataSet转换成Json格式      
            /// </summary>      
            /// <param name="ds">DataSet</param>      
            /// <returns></returns>      
            public static string ToJson(DataSet dseJson)
            {
                StringBuilder sbuBuilder = new StringBuilder();
                sbuBuilder.Append("{"Tables":[");
                foreach (DataTable dtJson in dseJson.Tables)
                {
                    sbuBuilder.Append(ToJson(dtJson) + ",");
                }
                sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                sbuBuilder.Append("]}");
    
                string strJson = sbuBuilder.ToString();
                strJson = strJson.Replace("
    ", "<br />");
                strJson = strJson.Replace("
    ", "<br />");
                return strJson;//sbuBuilder.ToString();
            }
            #endregion
    
    
    
            /// <summary>
            /// 带表头的
            /// </summary>
            /// <param name="dtaJson"></param>
            /// <param name="b"></param>
            /// <returns></returns>
            public static string ToJson(DataTable dtaJson, bool b)
            {
                StringBuilder sbuBuilder = new StringBuilder();
                sbuBuilder.Append("{"Rows":[");
                for (int i = 0; i < dtaJson.Rows.Count; i++)
                {
                    sbuBuilder.Append("{");
                    for (int j = 0; j < dtaJson.Columns.Count; j++)
                    {
    
                        sbuBuilder.Append(""" + dtaJson.Columns[j].ColumnName + "":");
                        sbuBuilder.Append(""");
                        sbuBuilder.Append(dtaJson.Rows[i][j].ToString().Replace(""", "\"").Replace("
    ", "<br>"));
                        sbuBuilder.Append("",");
                    }
                    sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                    sbuBuilder.Append("},");
                }
                if (dtaJson.Rows.Count > 0)
                {
                    sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                }
                sbuBuilder.Append("]}");
    
                string strJson = sbuBuilder.ToString();
                strJson = strJson.Replace("
    ", "<br />");
                strJson = strJson.Replace("
    ", "<br />");
    
                return strJson;//sbuBuilder.ToString();
            }
    
            /// <summary>
            /// 带表头的
            /// </summary>
            /// <param name="dseJson"></param>
            /// <returns></returns>
            public static string ToJson(DataSet dseJson, bool b)
            {
                StringBuilder sbuBuilder = new StringBuilder();
                sbuBuilder.Append("{"Tables":[");
                foreach (DataTable dtJson in dseJson.Tables)
                {
                    sbuBuilder.Append(ToJson(dtJson, true) + ",");
                }
                sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                sbuBuilder.Append("]}");
    
    
                string strJson = sbuBuilder.ToString();
                strJson = strJson.Replace("
    ", "<br />");
                strJson = strJson.Replace("
    ", "<br />");
                return strJson;//sbuBuilder.ToString();
            }
    
    
    
            /// <summary>
            /// 将JSON解析成DataSet只限标准的JSON数据
            /// 例如:Json={t1:[{name:'数据name',type:'数据type'}]} 
            /// 或 Json={t1:[{name:'数据name',type:'数据type'}],t2:[{id:'数据id',gx:'数据gx',val:'数据val'}]}
            /// </summary>
            /// <param name="Json">Json字符串</param>
            /// <returns>DataSet</returns>
            public static DataSet JsonToDataSet(string Json)
            {
                try
                {
                    DataSet ds = new DataSet();
                    JavaScriptSerializer JSS = new JavaScriptSerializer();
    
    
                    object obj = JSS.DeserializeObject(Json);
                    Dictionary<string, object> datajson = (Dictionary<string, object>)obj;
    
    
                    foreach (var item in datajson)
                    {
                        DataTable dt = new DataTable(item.Key);
                        object[] rows = (object[])item.Value;
                        foreach (var row in rows)
                        {
                            Dictionary<string, object> val = (Dictionary<string, object>)row;
                            DataRow dr = dt.NewRow();
                            foreach (KeyValuePair<string, object> sss in val)
                            {
                                if (!dt.Columns.Contains(sss.Key))
                                {
                                    dt.Columns.Add(sss.Key.ToString());
                                    dr[sss.Key] = sss.Value;
                                }
                                else
                                    dr[sss.Key] = sss.Value;
                            }
                            dt.Rows.Add(dr);
                        }
                        ds.Tables.Add(dt);
                    }
                    return ds;
                }
                catch
                {
                    return null;
                }
            }
    
    
    
    
            /// <summary>
            /// 临时方法 把字典表生成js文件 职位
            /// </summary>
            public static string funTemp(string total)
            {
                DataSet ds = hrSystem.DAL.commd.dalcomd.P_Sys_getTableCoum("zh_Sys_Position", "dictNO,parentNO, Explainss ", "intType", total);
                DataTable dt = ds.Tables[0];
                DataTable CurrDt = dt;
                StringBuilder sbuBuilder = new StringBuilder();
                sbuBuilder.Append("{"Item":[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["parentNO"] == DBNull.Value)
                    {
    
                        DataRow[] drTemp1 = dt.Select(" parentNO='" + dt.Rows[i]["dictNO"].ToString() + "'");
    
                        sbuBuilder.Append("[");
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            sbuBuilder.Append(""");
                            sbuBuilder.Append(dt.Rows[i][j].ToString().Replace(""", "\"").Replace("
    ", "<br>"));
                            sbuBuilder.Append("",");
                        }
                        if (dt.Rows[i]["parentNO"] == DBNull.Value)
                        {
    
                            DataTable dtTemp2 = new DataTable();
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                dtTemp2.Columns.Add("");
                            }
                            foreach (DataRow drTemp in dt.Select(" parentNO='" + dt.Rows[i]["dictNO"].ToString() + "'"))
                            {
                                string[] dtAppend = new string[drTemp.ItemArray.Length];
                                for (int k = 0; k < drTemp.ItemArray.Length; k++)
                                {
                                    dtAppend[k] = drTemp.ItemArray[k].ToString();
                                }
                                dtTemp2.Rows.Add(dtAppend);
                            }
    
                            sbuBuilder.Append(ToJson(dtTemp2) + "}");
                            dtTemp2.Clear();
                        }
    
                        sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                        sbuBuilder.Append("],,");
    
                        if (dt.Rows.Count > 0)
                        {
                            sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                        }
    
    
                    }
    
                }
                sbuBuilder.Append("]},");
                sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                return sbuBuilder.ToString();
            }
    
    
            /// <summary>
            /// 临时方法 把字典表生成js文件 城市
            /// </summary>
            public static string funTemp1()
            {
                DataSet ds = hrSystem.DAL.commd.dalcomd.P_Sys_getTableCoum("zh_Sys_crty", "intCrtyCode,intParentCrtyCode, strName,intType,strSpell ", "1", "1");
                DataTable dt = ds.Tables[0];
                DataTable CurrDt = dt;
                StringBuilder sbuBuilder = new StringBuilder();
                sbuBuilder.Append("{"Item":[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["intParentCrtyCode"].ToString() == "-1")
                    {
    
                        DataRow[] drTemp1 = dt.Select(" intParentCrtyCode='" + dt.Rows[i]["intCrtyCode"].ToString() + "'");
    
                        sbuBuilder.Append("[");
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            sbuBuilder.Append(""");
                            sbuBuilder.Append(dt.Rows[i][j].ToString().Replace(""", "\"").Replace("
    ", "<br>"));
                            sbuBuilder.Append("",");
                        }
                        if (dt.Rows[i]["intParentCrtyCode"].ToString() == "-1")
                        {
    
                            DataTable dtTemp2 = new DataTable();
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                dtTemp2.Columns.Add("");
                            }
                            foreach (DataRow drTemp in dt.Select(" intParentCrtyCode='" + dt.Rows[i]["intCrtyCode"].ToString() + "'"))
                            {
                                string[] dtAppend = new string[drTemp.ItemArray.Length];
                                for (int k = 0; k < drTemp.ItemArray.Length; k++)
                                {
                                    dtAppend[k] = drTemp.ItemArray[k].ToString();
                                }
                                dtTemp2.Rows.Add(dtAppend);
                            }
    
                            sbuBuilder.Append(ToJson(dtTemp2) + "}");
                            dtTemp2.Clear();
                        }
    
                        sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                        sbuBuilder.Append("],,");
    
                        if (dt.Rows.Count > 0)
                        {
                            sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                        }
    
    
                    }
    
                }
                sbuBuilder.Append("]},");
                sbuBuilder.Remove(sbuBuilder.Length - 1, 1);
                return sbuBuilder.ToString();
            }
        }

     ------------------------------------------------------第二种方式 自带的

     string json = JsonConvert.SerializeObject(ds.Tables[0]);
     List<MuzuAPI.Model.shopgoodsstock> list = JsonConvert.DeserializeObject<List<MuzuAPI.Model.shopgoodsstock>>(json);

     ------------------------------------------------------

    /// <summary>
        /// DataTable转实体集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static class FillModel<T> where T : new()
        {
            /// <summary>  
            /// 填充对象列表:用DataTable填充实体类
            /// </summary>  
            public static List<T> TableToModel(DataTable dt)
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;
                }
                List<T> modelList = new List<T>();
                foreach (DataRow dr in dt.Rows)
                {
                    T model = new T();
                    for (int i = 0; i < dr.Table.Columns.Count; i++)
                    {
                        PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                        if (propertyInfo != null && dr[i] != DBNull.Value)
                            propertyInfo.SetValue(model, dr[i], null);
                    }
    
                    modelList.Add(model);
                }
                return modelList;
            }
  • 相关阅读:
    使用博客园平台写文章赚外快的实践
    博客换来的不仅仅是评论,还有Money!!!
    软件测试方法和规则
    向string,int,类对象等中扩展方法
    江苏省计算机三级偏软知识点整理
    MVC是什么
    ASP.NET关于session的用法
    ASP.Net 中Application的用法
    什么是单件模式
    输入法中全角和半角的区别
  • 原文地址:https://www.cnblogs.com/sxmny/p/3213478.html
Copyright © 2020-2023  润新知