当前位置:首页 >.NET > 正文内容

DataTable转换成Json格式

大滑稽11年前 (2014-03-24).NET956
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write(DataTableToJson(MyTable()));
            Console.Read();
        }

         //"{\"totalpage\":\"10\",\"data\":[\"pic1\",\"pic2\",\"pic\"]}"
         // {totalpage:10,data:[pic1,pic2,pic]}
        public static string DataTableToJson(DataTable dt)
        {
            string columnFirst = "";
            List<string> result=new List<string>();
            StringBuilder Json = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (columnFirst != dt.Rows[i][0].ToString())
                    {
                        if (i != 0)
                        {
                            AddNewJson(Json, result, dt);
                        }
                        columnFirst = dt.Rows[i][0].ToString();
                        result = new List<string>();
                        for (int k = 0; k < dt.Columns.Count; k++)
                        {
                            result.Add("\"" + dt.Rows[i][k].ToString() + "\"");
                        }
                    }
                    else
                    {
                        for (int k = 0; k < dt.Columns.Count; k++)
                        {
                            if (!result[k].Contains(dt.Rows[i][k].ToString()))
                            {
                                result[k] += ",\"" + dt.Rows[i][k].ToString()+"\"";
                            }
                        }
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        AddNewJson(Json, result, dt);
                    }
                }
            }
            return Json.ToString();
        }

        private static void AddNewJson(StringBuilder Json,List<string> result,DataTable dt)
        {
            Json.Append("{");
            for (int i = 0; i < dt.Columns.Count;i++ )
            {
                Json.Append("\"");
                Json.Append(dt.Columns[i].ColumnName);
                Json.Append("\":");
                if (result[i].Contains(","))
                {
                    Json.Append("[");
                    Json.Append(result[i]);
                    if (i == dt.Columns.Count - 1)
                    {
                        Json.Append("]");
                    }
                    else
                    {
                        Json.Append("],");
                    }
                }
                else
                {
                    Json.Append(result[i]);
                    if (i != dt.Columns.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("}");
        }

        private static DataTable MyTable()
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn();
            dc.ColumnName = "ID";
            dc.DataType = typeof(String);
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.ColumnName = "Name";
            dc.DataType = typeof(String);
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.ColumnName = "Score";
            dc.DataType = typeof(String);
            dt.Columns.Add(dc);
            DataRow dr = dt.NewRow();
            dr["ID"] = "01";
            dr["Name"] = "Jin";
            dr["Score"] = "10";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "01";
            dr["Name"] = "Xin";
            dr["Score"] = "20";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "01";
            dr["Name"] = "Xu";
            dr["Score"] = "30";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "02";
            dr["Name"] = "Xin";
            dr["Score"] = "30";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "02";
            dr["Name"] = "Jin";
            dr["Score"] = "30";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "02";
            dr["Name"] = "Jin";
            dr["Score"] = "30";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "02";
            dr["Name"] = "Jin";
            dr["Score"] = "40";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "02";
            dr["Name"] = "Jin";
            dr["Score"] = "30";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "02";
            dr["Name"] = "Jin";
            dr["Score"] = "30";
            dt.Rows.Add(dr);
            return dt;
        }
    }
}


扫描二维码推送至手机访问。

版权声明:本文由第★一★次发布,如需转载请注明出处。

本文链接:https://wpers.net/post/34.html

“DataTable转换成Json格式” 的相关文章

Cbo控件数据源绑定

 //Cbo控件数据源绑定DataTable DtType = noteType.GetTypeList("");         ...

Linq读写XML

         private List<News> GetNews(string html)    &n...

修改注册表限制软件使用次数

 private void Form1_Load(object sender, System.EventArgs e){RegistryKey RootKey,RegKey; //项名为:HKEY_CURRENT_USER\So...

c#中分页显示数据

     //c#中分页显示数据    public partial class Form1 : Form    {  ...

C#修改浏览器主页

string key = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main";      &n...

C#获得程序集

 //获得程序集System.Reflection.Assembly assem = System.Reflection.Assembly.GetExecutingAssembly();...