C# DataTable转Json

      /// <summary>
        /// 将datatable转换为json  
        /// </summary>
        /// <param name="dtb">Dt</param>
        /// <returns>JSON字符串</returns>
        public static string GetJsonByDataTable(DataTable dtb)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            System.Collections.ArrayList dic = new System.Collections.ArrayList();
            foreach (DataRow dr in dtb.Rows)
            {
                System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
                foreach (DataColumn dc in dtb.Columns)
                {

                    if (dr[dc.ColumnName].GetType().Name.Equals("DateTime"))
                        drow.Add(dc.ColumnName, Convert.ToString(dr[dc.ColumnName]));
                    else
                        drow.Add(dc.ColumnName, dr[dc.ColumnName]);

                }
                dic.Add(drow);

            }
            //序列化  
            return jss.Serialize(dic);
        }

C# DataTable转Json

上一篇:win10 Redis闪退问题


下一篇:C#_使用 linq 删除 List 列表中重复项的三种方法