asp.net如何将DataSet转换成josn并输出
public class JsonUtil
{
public string ToJson(DataSet dataSet)
{
string jsonString = "{";
foreach (DataTable table in dataSet.Tables)
{
jsonString += """" + table.TableName + """:" + ToJson(table) + ",";
}
jsonString = jsonString.TrimEnd(',');
return jsonString + "}";
}
public string ToJson(DataTable dt)
{
StringBuilder jsonString = new StringBuilder();
jsonString.Append("[");
DataRowCollection drc = dt.Rows;
for (int i = 0; i < drc.Count; i++)
{
jsonString.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
string strKey = dt.Columns[j].ColumnName;
string strValue = drc[i][j].ToString();
Type type = dt.Columns[j].DataType;
jsonString.Append("""" + strKey + """:");
strValue = String.Format(strValue, type);
jsonString.Append("""" + strValue + """,");
}
jsonString.Append("},");
}
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append("]");
return jsonString.ToString();
}
}
复制代码使用JsonUtil ju = new JsonUtil();
Response.Write(ju.ToJson(ds));
复制代码如果解析有问题可以把代码中的""""用"\""替换
asp.net如何将DataSet转换成josn并输出的更多相关文章
- 把DataSet转换成JSON
/// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"> ...
- DataSet转换成List<>
方法一: //DataSet转换成List<ArticleInfo> public List<ArticleInfo> GetArticleList(DataSet ds) { ...
- .net 数据源DataSet 转换成模型
/// <summary> /// DataSet转换成model 自动赋值返回集合 /// </summary> /// <typeparam name="T ...
- c#实现list,dataset,DataTable转换成josn等各种转换方法总和
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Refle ...
- 将DataSet转换成json
/// <summary> /// 把dataset数据转换成json的格式 /// </summary> /// <para ...
- dataTable/dataSet转换成Json格式
using System.Text;using System.Collections.Generic; 1)dataTable转Json(表格有名称:dt.TableName) public stat ...
- Asp.net C# 把 Datatable转换成JSON 字符串
First of all, we have to fetch the records from the database (MS Sqlserver) into the C# DataTable, o ...
- JS 解决json字符串转换成json树形输出
问题: 后台获取一个字符串,格式为 string +jsonList+string+..... 就是传过来一堆数据,然后其中包含了一个json格式的list, 我们希望能将它输出成树形结构显示,能够 ...
- java代码将excel文件中的内容列表转换成JS文件输出
思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...
随机推荐
- ScannerDemo------string int
import java.util.Scanner; /** *Scanner演示 */ public cl ...
- parent children
class parent{ protected static int count=0; public parent() { count++; } } public class child extend ...
- java:定义线程
Thread是java.lang包的类,默认导入. 进程:操作系统中的程序,多进程即同时运行多个程序.线程:程序中的流,多线程即程序中有多个流同时执行. 一个线程用一个线程对象表示 创建线程的方法: ...
- brew命令
下面参考下网友的总结: 查看brew的帮助 brew –help 安装软件 brew install git 卸载软件 brew uninstall git 搜索软件 brew search git ...
- 用Telnet发送HTTP请求
1. 进入cmd命令环境 2. 输入"telnet www.baidu.com 80" 3. 利用快捷键"Ctrl+](右中括号)"来打开本地回显功能 (注本阶 ...
- 9、JPA_映射双向一对一的关联关系
双向一对一的关联关系 举例说明:经理Manager和部门Department是双向一对一关联关系.则Manager实体类中有Department实体对象的引用,反之亦然. 其实体属性定义如下: Lis ...
- 2、@RequestMapping注解的用法
@RequestMapping有如下属性值:
- UVa 1402 Runtime Error 伸展树
Runtime Error 到现在连样例也跑不出来!!! 调试了一晚上快要死了…… 知道错在哪里但是不会改,代码先扔在这里吧.看来不能太依赖模板啊orz…… #include <cstdio&g ...
- oracle tns in linux
[oracle@redhat4 admin]$ cd $ORACLE_HOME/network/admin[oracle@redhat4 admin]$ cat tnsnames.ora# tnsna ...
- Android开发之源码:多次点击事件的原理和实现
多次点击事件 多次点击事件原理:最后一次点击事件与第一次点击事件的时间间隔是否小于某个时间,当小于的时候,就认为这是一个多次点击事件. Android源码实现效果: import android.ap ...