将DataSet里的数据转换成Json格式
2010-05-15 15:14
393 查看
转:http://www.cnblogs.com/simonblog/archive/2010/05/12/1733139.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
public static class DataSetToJson
{
public static string DSToJson(this DataSet ds, string JsonName, string[] ParName)
{
try
{
if (ds == null)
{
return "DataSet Is Null ,So I Can't Do It To Json!";
}
if (JsonName.Length < 1)
{
return "You Set The Json Name Is Wrong!";
}
if (ds.Tables[0].Columns.Count < ParName.Length)
{
return "You Give The ParName Is Bigger Than DataSet Columns!";
}
string josn = "{" + JsonName + ":[";
string temp = "";
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
temp = temp + "{";
for (int i = 0; i < ParName.Length; i++)
{
temp += "" + ParName[i] + ":\'" + ds.Tables[0].Rows[j][ParName[i]] + "\'";
if (i != ParName.Length - 1)
{
temp = temp + ",";
}
}
if (j == ds.Tables[0].Rows.Count - 1)
{
temp = temp + "}";
}
else
{
temp = temp + "},";
}
}
josn = josn + temp + "]}";
return josn;
}
catch (Exception ex)
{
return "Codeing is Error----" + ex.ToString();
}
}
}
}
这是一种对数据取出的数据进行转换成json格式,还有一种就是可以把数据库里的数据存入到一个IList<>里,然后通过json里的JsonWriter来转换成json格式,这里就不显示代码了,,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
public static class DataSetToJson
{
public static string DSToJson(this DataSet ds, string JsonName, string[] ParName)
{
try
{
if (ds == null)
{
return "DataSet Is Null ,So I Can't Do It To Json!";
}
if (JsonName.Length < 1)
{
return "You Set The Json Name Is Wrong!";
}
if (ds.Tables[0].Columns.Count < ParName.Length)
{
return "You Give The ParName Is Bigger Than DataSet Columns!";
}
string josn = "{" + JsonName + ":[";
string temp = "";
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
temp = temp + "{";
for (int i = 0; i < ParName.Length; i++)
{
temp += "" + ParName[i] + ":\'" + ds.Tables[0].Rows[j][ParName[i]] + "\'";
if (i != ParName.Length - 1)
{
temp = temp + ",";
}
}
if (j == ds.Tables[0].Rows.Count - 1)
{
temp = temp + "}";
}
else
{
temp = temp + "},";
}
}
josn = josn + temp + "]}";
return josn;
}
catch (Exception ex)
{
return "Codeing is Error----" + ex.ToString();
}
}
}
}
这是一种对数据取出的数据进行转换成json格式,还有一种就是可以把数据库里的数据存入到一个IList<>里,然后通过json里的JsonWriter来转换成json格式,这里就不显示代码了,,
相关文章推荐
- JSON 的数据转换格式(DataTable或DataSet) -善良公社项目
- ASP.NET DataSet查询结果转换为JSON格式数据
- 把dataset数据转换成json的格式通用方法
- JSON 的数据转换格式(DataTable或DataSet) -善良公社项目
- C#将DataSet或DataTable转换成Json格式数据传递到前台JS脚本
- dataset数据转换成json的格式通用方法
- 把dataset数据转换成json的格式通用方法
- dataset数据转换成json的格式(转)
- JSONObject.toBean中日期格式数据转换失效的解决办法! .
- Python将JSON格式数据转换为SQL语句以便导入MySQL数据库
- 数据库中查询出来的数据转换成json格式
- fastJson数据格式转换(二)SerializerFeature属性再解
- 使用Javascript/jQuery将javascript对象转换为json格式数据
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- List<Map<String, Object>> 转换成JSON数据格式保存再转List<Map<String, Object>> 返回
- jsp页面把获取的map数据转换成json格式的数据来使用
- var dataObj=eval("("+data+")");//转换为json对象(解决在ajax返回json格式数据的时候明明正确的获取了返回值但是却就是进不去success方法的问题。格式错误)
- JavaScript和json对数据格式的转换
- 【Java中将JSON格式的数据转换成对应的Bean、Map、List数据】
- Java中将JSON格式的数据转换成对应的Bean、Map、List数据