将Datatable转换为Json数据

Datatable dt=new Datatable();

StringBuilder JsonString = new StringBuilder();
//Exception Handling
if (dt != null && dt.Rows.Count > 0)
{
JsonString.Append("{ ");
JsonString.Append("\"T_blog\":[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
JsonString.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
}
else if (j == dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
}
}
/**/
/**/
/**/
/*end Of String*/
if (i == dt.Rows.Count - 1)
{
JsonString.Append("} ");
}
else
{
JsonString.Append("}, ");
}
}
JsonString.Append("]}");
return JsonString.ToString();
}
else
{
return null;
}

时间: 2024-10-17 22:41:17

将Datatable转换为Json数据的相关文章

Datatable转换为Json 然后把Json数据放入 js 文件中

C#中把Datatable转换为Json的5个代码实例 /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable dt) { StringBuilde

C#中把Datatable转换为Json的5个代码实例

一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable dt) { StringBuilder jsonString = new String

iOS 本地可以构造假数据,并转换为json数据

// 当服务端接口没做好时,本地可以构造假数据,并转换为json数据 NSError *error; NSString *dataStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"indEditor" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error]; NSData *jsonD

ashx将datatable返回json数据

1.直接使用JsonConvert.SerializeObject().将datatable放入  输出字符串 下面是测试:用webform+ashx作为接口. public class GetJson : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/json"; var str = DataTableToJsonWithJso

字典型转换为JSON数据

一)将NSDictionary转换成为NSData类型 NSDictionary *tempDict=[[NSDictionary alloc] initWithObjectsAndKeys:@"nihao",@"sayHeiio", @"woshizhu",@"geyan",nil]; NSData *jsonData=[self  toJSONData:tempDict]; 二)将NSData类型转换为Json NSStr

DataTable转换为Json格式

public string DataTableToJson(string jsonName, DataTable dt, TemplateFilter temp)         { StringBuilder Json = new StringBuilder(); Json.Append("["); Json.Append("{"); //当前页码 Json.Append("\"pageIndex\":\"" +

DataTable转换为JSON

最后的格式为:[{},{},...] StringBuilder DataTableToJSON(DataTable dt) { string columnName; StringBuilder builder = new StringBuilder(); if (dt != null) { builder.Append("["); for (int i = 0; i < dt.Rows.Count; i++) { builder.Append("{"); f

从DataTable获取Json数据

public string GetJson(DataTable dt){ JavaScriptSerializer jss=new JavaScriptSerializer(); jss.MaxJsonLength=Int32.MaxValue; System.Collections.Arraylist dic=new System.Collections.ArrayList(); foreach(DataRow dr  in dt.Rows){ System.Collections.Gener

URL编码数据转换为JSON数据

NSString *urlString; urlString=[self    URLDecodedString:urlString]; -(NSString *)URLDecodedString:(NSString*)str { return (__bridge NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)str, CFSTR("&quo