惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 行知

解决DOC-to-Help Import CHM 文件的TOC错误问题 Teradata Expression 12 在Windows 2003上Connection Reset 问题的解决方法 SQL Server 链接Oracle数据库的查询 openSUSE 添加一个Application OpenSUSE+Eclipse+Aptana+Air的安装过程全纪录 WCF Host Open的问题 在Linux系统中安装VMWare Tools 在RedHat Enterprise 4 上安装 Mono1.9 (四) 在RedHat Enterprise 4 上安装 Mono1.9 (三) 在RedHat Enterprise 4 上安装 Mono1.9 (二) 在RedHat Enterprise 4 上安装 Mono1.9 (一) ActiveRecord 对象在Json序列化时,出现异常Newtonsoft.Json.JsonSerializationException: Self referencing loop ExtJs+MonoRail 使用XML传递数据 Composite UI Application Block and DevExpress log4net碰到的奇怪问题 继续招聘.net程序员(上海) 招聘.Net程序员(工作在上海) 有感于框架设计难,实施框架更难! NHibernate日期类型的映射
DataTable 的 JSON 序列化
行知 · 2007-11-24 · via 博客园 - 行知

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

 1public 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        }