C# DataTable转换成JSON数据
2016-01-13 17:29
597 查看
public string DataTableToJson(DataTable dt)
{
string str = "";
if (dt.Rows.Count > 0)
{
string[] titlecolumn = new string[dt.Columns.Count];
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
titlecolumn[i] = dt.Columns[i].ColumnName;
}
for (int z = 0; z <= dt.Rows.Count - 1; z++)
{
str += "{";
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
if (j < (dt.Columns.Count - 1))
{
str += "\"" + titlecolumn[j] + "\":\"" + dt.Rows[z][j].ToString() + "\",\n";
}
else
{
if (z < (dt.Rows.Count - 1))
str += "\"" + titlecolumn[j] + "\":\"" + dt.Rows[z][j].ToString() + "\"},\n";
else
str += "\"" + titlecolumn[j] + "\":\"" + dt.Rows[z][j].ToString() + "\"}\n";
}
}
}
}
return "[" + str + "]";
}
{
string str = "";
if (dt.Rows.Count > 0)
{
string[] titlecolumn = new string[dt.Columns.Count];
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
titlecolumn[i] = dt.Columns[i].ColumnName;
}
for (int z = 0; z <= dt.Rows.Count - 1; z++)
{
str += "{";
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
if (j < (dt.Columns.Count - 1))
{
str += "\"" + titlecolumn[j] + "\":\"" + dt.Rows[z][j].ToString() + "\",\n";
}
else
{
if (z < (dt.Rows.Count - 1))
str += "\"" + titlecolumn[j] + "\":\"" + dt.Rows[z][j].ToString() + "\"},\n";
else
str += "\"" + titlecolumn[j] + "\":\"" + dt.Rows[z][j].ToString() + "\"}\n";
}
}
}
}
return "[" + str + "]";
}
相关文章推荐
- js非空判断
- alert 替代效果smoke.js
- JSON数据理解
- 《JS高级程序设计》读书笔记
- 详解JavaScript中localStorage使用要点
- 上传插件webuploader.js的使用示例
- jsoncpp 构造空数组
- javascript图片切换综合实例(循环切换、顺序切换)
- jstack(查看线程)、jmap(查看内存)和jstat(性能分析)命令
- 在线演示红黑树(javascript)
- JS 一定要放在 Body 的最底部么?聊聊浏览器的渲染机制
- JS 判断 Radio 单选按钮是否为选中状态 并弹出 值信息
- ExtJS4 - 文本框之 maxLength ,阻止用户输入过多的字符
- 从接口读取数据,Json解析后显示在RecyclerView上
- Javascript学习笔记04 ——变量,作用域,内存问题
- JSHint 配置浅析
- js获取select标签选中的值
- js 获取当月天数
- JS继承
- JavaScript小经验小工具-总结一