DataTable 的 JSON 序列化

随着AJAX,MVC等WEB框架的使用,JavaScript又更多的回到了我们身边。
在JS中我们需要对对象进行JSON序列化通常使用JSON.net, 不过它对DataTable的序列化不能很好的满足的我们的需求,后来在CodeProject发现一个兄弟已经写好了 Convert ASP.NET DataTable to JSON, to use datatable in JAVASCRIPT ,记一下,免得忘记了。

 1 public   string  CreateJsonParameters(DataTable dt)
 2          {
 3            /**//* /****************************************************************************
 4             * Without goingin to the depth of the functioning of this Method, i will try to give an overview
 5             * As soon as this method gets a DataTable it starts to convert it into JSON String,
 6             * it takes each row and in each row it grabs the cell name and its data.
 7             * This kind of JSON is very usefull when developer have to have Column name of the .
 8             * Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
 9             * NOTE: One negative point. by this method user will not be able to call any cell by its index.
10             * *************************************************************************/

11            StringBuilder JsonString = new StringBuilder();
12            //Exception Handling        
13            if (dt != null && dt.Rows.Count > 0)
14            {
15                JsonString.Append("");
16                JsonString.Append("\"Head\":[ ");
17                for (int i = 0; i < dt.Rows.Count; i++)
18                {
19                    JsonString.Append("");
20                    for (int j = 0; j < dt.Columns.Count; j++)
21                    {
22                        if (j < dt.Columns.Count - 1)
23                        {
24                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
25                        }

26                        else if (j == dt.Columns.Count - 1)
27                        {
28                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
29                        }

30                    }

31                    /**//*end Of String*/
32                    if (i == dt.Rows.Count - 1)
33                    {
34                        JsonString.Append("");
35                    }

36                    else
37                    {
38                        JsonString.Append("}, ");
39                    }

40                }

41                JsonString.Append("]}");
42                return JsonString.ToString();
43            }

44            else
45            {
46                return null;
47            }

48        }

转载于:https://www.cnblogs.com/xingzhixp/archive/2007/11/24/971422.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值