c# datatable 转换成json格式

csharp

浏览数:223

2019-1-7

AD:资源代下载服务

片段 1片段 2片段 3


json格式

 public static string CreateJsonParameters(DataTable dt)
        {
            StringBuilder JsonString = new StringBuilder();
            //Exception Handling        
            if (dt != null && dt.Rows.Count > 0)
            {
                 JsonString.Append("[ ");

                string name, value;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            name = dt.Columns[j].ColumnName.ToString();
                            value = dt.Rows[i][j].ToString();
                            value = value.Replace("\n", "\\r\\n");
                            JsonString.Append("\"" + name + "\":" + "\"" + value + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            name = dt.Columns[j].ColumnName.ToString();
                            value = dt.Rows[i][j].ToString();
                            value = value.Replace("\n", "\\r\\n");
                            JsonString.Append("\"" + name + "\":" + "\"" + value + "\"");
                        }
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }

                }
                  JsonString.Append("] ");

                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }


2

 public static string Table2GridJson(DataTable dt, int totalCount)
        {
            StringBuilder JsonString = new StringBuilder();
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"total\":" + totalCount + ",");
                JsonString.Append("\"rows\":[ ");
                string name, value;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            name = dt.Columns[j].ColumnName.ToString();
                            value = dt.Rows[i][j].ToString();
                            value = value.Replace("\n", "\\r\\n");
                            JsonString.Append("\"" + name + "\":" + "\"" + value + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            name = dt.Columns[j].ColumnName.ToString();
                            value = dt.Rows[i][j].ToString();
                            value = value.Replace("\r\n", "\\r\\n");
                            JsonString.Append("\"" + name + "\":" + "\"" + value + "\"");
                        }
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]}");
                return JsonString.ToString();
            }
            else
            {
                return "{\"total\":0,\"rows\":[]}";
            }
        }


LIST

 public void GetFamilyListHuaao(string MakeCode)
        {
            Code.Cars cars = new Code.Cars();
            var result = string.Empty;
            var seriesList = new List<Series>();

            System.Data.DataTable dt = cars.GetVehicleFamilyListOrder(MakeCode);

            foreach (DataRow dr in dt.Rows)
            {
                var series = new Series()
                {
                    SeriesCode = dr["FamilyCode"].ToString(),
                    CarSeries = dr["DescriptionChinese"].ToString()
                };
                seriesList.Add(series);
            }

            result = JsonConvert.SerializeObject(seriesList);

            System.Web.HttpContext.Current.Response.Write(result);
        }