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

推荐订阅源

V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Latest
Security Latest
H
Hacker News: Front Page
T
Tenable Blog
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Project Zero
Project Zero
O
OpenAI News
AI
AI
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
NISL@THU
NISL@THU
A
Arctic Wolf
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
U
Unit 42
S
Security Affairs
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
月光博客
月光博客
Engineering at Meta
Engineering at Meta
腾讯CDC
F
Full Disclosure
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
The Cloudflare Blog

博客园 - 天天无用

jsonp原理实例及mvc中的应用 ubuntu笔记 修复MVC3中使用Remote验证的一点小问题 诺基亚手机S60系统证书申请、软件签名图文教程 JAXER留言板-一个html页面的ajax留言版 - 天天无用 - 博客园 初探JAXER Jquery插件研究:Ajax File Upload 本月wii游戏太多,感觉有点堕落 使用jQuery加DIV实现可以动态添加的金字塔结构 自制的操作下拉列表框(SELECT)的三个jquery插件(ajax填充、联动、增加选项) - 天天无用 - 博客园 Asp.net 2.0 TreeView控件使用jQuery无刷新添加节点详细说明 用jQuery实现.net 2.0 treeview客户端无刷新操作的实例 基于jQuery的AJAX和JSON实现纯html数据模板 自己写的jQuery自动完成的插件(AutoComplete) asp.net treeview控件无刷新选择和删除节点(使用jquery) - 天天无用 - 博客园 纵向合并gridview单元格的两种方法 .net下两种json序列化速度比对 asp.net 2.0 中 TreeView控件中的checkbox客户端操作 asp.net 2.0中根据roles显示不同的sitemap - 天天无用 - 博客园
自己写的DataTable转换成JSON字符串的函数
天天无用 · 2007-11-14 · via 博客园 - 天天无用

    最近一直要用到JSON数据,而且是要把DataTable转换成JSON。以前一直是用Newtonsoft.Json.dll序列化,先要把DataTable转换成xml格式,然后再用Newtonsoft.Json.dll转换xml格式到json格式。(见以前的文章http://blog.csdn.net/luq885/archive/2007/05/10/1603330.aspx

    其实JSON数据的格式比较简单,仔细研究了一下,发现直接分解DataTable,然后用StringBuilder来构造成JSON格式也不是很复杂。函数代码如下:

    private string DataTable2Json(DataTable dt)
    
{
        StringBuilder jsonBuilder 
= new StringBuilder();
        jsonBuilder.Append(
"{\"");
        jsonBuilder.Append(dt.TableName);
        jsonBuilder.Append(
"\":[");
        
for (int i = 0; i < dt.Rows.Count; i++)
        
{
            jsonBuilder.Append(
"{");
            
for (int j = 0; j < dt.Columns.Count; j++)
            
{
                jsonBuilder.Append(
"\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append(
"\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append(
"\",");
            }

            jsonBuilder.Remove(jsonBuilder.Length 
- 11);
            jsonBuilder.Append(
"},");
        }

        jsonBuilder.Remove(jsonBuilder.Length 
- 11);
        jsonBuilder.Append(
"]");
        jsonBuilder.Append(
"}");
        
return jsonBuilder.ToString();
    }

    这一行“jsonBuilder.Remove(jsonBuilder.Length - 1, 1);”是为了去掉最后一个多于的逗号,当然这个函数不能操作比较包含复杂数据类型的DataTable,不过对于现在的工作已经够用了。