代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
DATAtable转换成 json格式
作者:
Dezai.CN
/ 发布于
2011/8/5
/
777
<div> 将DATAtable转换成 json格式:DataTableConvertJson.cs <div style="width: 93.54%; height: 1780px" class="cnblogs_code"> <pre><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> DataTableConvertJson { </span><span style="color: #0000ff">#region</span><span style="color: #000000"> dataTable转换成Json格式</span><span style="color: #000000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> dataTable转换成Json格式 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="dt"></param></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><returns></returns></span><span style="color: #008000"> </span><span style="color: #808080"></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> DataTable2Json(DataTable dt) { StringBuilder jsonBuilder </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> StringBuilder(); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">{\"</span><span style="color: #800000">"</span><span style="color: #000000">); jsonBuilder.Append(dt.TableName); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">\":[</span><span style="color: #800000">"</span><span style="color: #000000">); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">[</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> dt.Rows.Count; i</span><span style="color: #000000">++</span><span style="color: #000000">) { jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">{</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> j </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; j </span><span style="color: #000000"><</span><span style="color: #000000"> dt.Columns.Count; j</span><span style="color: #000000">++</span><span style="color: #000000">) { jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">\"</span><span style="color: #800000">"</span><span style="color: #000000">); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">\":\"</span><span style="color: #800000">"</span><span style="color: #000000">); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">\",</span><span style="color: #800000">"</span><span style="color: #000000">); } jsonBuilder.Remove(jsonBuilder.Length </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">, </span><span style="color: #800080">1</span><span style="color: #000000">); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">},</span><span style="color: #800000">"</span><span style="color: #000000">); } jsonBuilder.Remove(jsonBuilder.Length </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">, </span><span style="color: #800080">1</span><span style="color: #000000">); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">]</span><span style="color: #800000">"</span><span style="color: #000000">); jsonBuilder.Append(</span><span style="color: #800000">"</span><span style="color: #800000">}</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000"> jsonBuilder.ToString(); } </span><span style="color: #0000ff">#endregion</span><span style="color: #000000"> dataTable转换成Json格式</span><span style="color: #000000"> </span><span style="color: #0000ff">#region</span><span style="color: #000000"> DataSet转换成Json格式</span><span style="color: #000000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> DataSet转换成Json格式 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="ds"></span><span style="color: #008000">DataSet</span><span style="color: #808080"></param></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><returns></returns></span><span style="color: #008000"> </span><span style="color: #808080"></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> Dataset2Json(DataSet ds) { StringBuilder json </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> StringBuilder(); </span><span style="color: #0000ff">foreach</span><span style="color: #000000"> (DataTable dt </span><span style="color: #0000ff">in</span><span style="color: #000000"> ds.Tables) { json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">{\"</span><span style="color: #800000">"</span><span style="color: #000000">); json.Append(dt.TableName); json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">\":</span><span style="color: #800000">"</span><span style="color: #000000">); json.Append(DataTable2Json(dt)); json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">}</span><span style="color: #800000">"</span><span style="color: #000000">); } </span><span style="color: #0000ff">return</span><span style="color: #000000"> json.ToString(); } </span><span style="color: #0000ff">#endregion</span><span style="color: #000000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> Msdn </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="jsonName"></param></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="dt"></param></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><returns></returns></span><span style="color: #808080"></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> DataTableToJson(</span><span style="color: #0000ff">string</span><span style="color: #000000"> jsonName, DataTable dt) { StringBuilder Json </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> StringBuilder(); Json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">{\"</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> jsonName </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">\":[</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (dt.Rows.Count </span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">) { </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> dt.Rows.Count; i</span><span style="color: #000000">++</span><span style="color: #000000">) { Json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">{</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> j </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; j </span><span style="color: #000000"><</span><span style="color: #000000"> dt.Columns.Count; j</span><span style="color: #000000">++</span><span style="color: #000000">) { Json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">\"</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> dt.Columns[j].ColumnName.ToString() </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">\":\"</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> dt.Rows[i][j].ToString() </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">\"</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (j </span><span style="color: #000000"><</span><span style="color: #000000"> dt.Columns.Count </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">) { Json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">,</span><span style="color: #800000">"</span><span style="color: #000000">); } } Json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">}</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (i </span><span style="color: #000000"><</span><span style="color: #000000"> dt.Rows.Count </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">) { Json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">,</span><span style="color: #800000">"</span><span style="color: #000000">); } } } Json.Append(</span><span style="color: #800000">"</span><span style="color: #800000">]}</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000"> Json.ToString(); } }</span></pre> </div> 调用 json<span style="color: #000000">=</span><span style="color: #000000"> DataTableConvertJson.DataTableToJson(</span><span style="color: #800000">"</span><span style="color: #800000">tble</span><span style="color: #800000">"</span><span style="color: #000000">, tblDatas);</span> <span style="color: #000000">//Quotation_No 为tblDatas的一列 </span>alert(msg.tble[i].Quotation_No) </div>
试试其它关键字
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3