编程语言
首页 > 编程语言> > C# DataTable、Json和List的互相转换

C# DataTable、Json和List的互相转换

作者:互联网

DataTable 转换 Json

Newtonsoft.Json

string json = JsonConvert.SerializeObject(dt);

自定义

        /// <summary>
        /// DataTable转换JSON
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string DataTableToJson(DataTable dt)
        {
            DataSet ds = new DataSet();
            ds.Merge(dt);
            StringBuilder JsonString = new StringBuilder();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                JsonString.Append("[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    JsonString.Append("{");
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                    {
                        if (j < ds.Tables[0].Columns.Count - 1)
                        {
                            JsonString.Append("\"" + ds.Tables[0].Columns[j].ColumnName.ToString() + "\":" + "\"" + ds.Tables[0].Rows[i][j].ToString() + "\",");
                        }
                        else if (j == ds.Tables[0].Columns.Count - 1)
                        {
                            JsonString.Append("\"" + ds.Tables[0].Columns[j].ColumnName.ToString() + "\":" + "\"" + ds.Tables[0].Rows[i][j].ToString() + "\"");
                        }
                    }
                    if (i == ds.Tables[0].Rows.Count - 1)
                    {
                        JsonString.Append("}");
                    }
                    else
                    {
                        JsonString.Append("},");
                    }
                }
                JsonString.Append("]");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }

Json 转换 DataTable

Newtonsoft.Json

string json = "[{\"ID\":\"1\",\"Name\":\"2\"}]";
DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);

List 转换 DataTable

Newtonsoft.Json

List<User> list = new List<User>();
string json = Newtonsoft.Json.JsonConvert.SerializeObject(list);
DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);

自定义

        /// <summary>
        /// List转换DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public DataTable ToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
                table.Columns.Add(props[i].Name, props[i].PropertyType);

            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

标签:Count,Tables,JsonString,C#,List,DataTable,Json,ds
来源: https://www.cnblogs.com/RainFate/p/16243706.html