DataTable 或者Ilist<>转JSON格式
2010-07-22 10:52
295 查看
using System; using System.Data; using System.Text; using System.Collections.Generic; using System.Reflection; /// <summary> /// 将DataTable或Ilist<>转换成JSON格式 /// </summary> public class ToJson { public ToJson() { } public static string DataTableToJson(string jsonName, DataTable dt) { StringBuilder Json = new StringBuilder(); Json.Append("{\"" + jsonName + "\":["); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { Json.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\""); if (j < dt.Columns.Count - 1) { Json.Append(","); } } Json.Append("}"); if (i < dt.Rows.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); } public static string ObjectToJson<T>(string jsonName, IList<T> IL) { StringBuilder Json = new StringBuilder(); Json.Append("{\"" + jsonName + "\":["); if (IL.Count > 0) { for (int i = 0; i < IL.Count; i++) { T bj = Activator.CreateInstance<T>(); Type type = obj.GetType(); PropertyInfo[] pis = type.GetProperties(); Json.Append("{"); for (int j = 0; j < pis.Length; j++) { Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\""); if (j < pis.Length - 1) { Json.Append(","); } } Json.Append("}"); if (i < IL.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); } }
相关文章推荐
- 将DataTable或Ilist<>转换成JSON格式
- datatable转换成list<T>,datatable转换成json格式
- 将DataTable或Ilist<>转换成JSON格式
- List<T>、DataSet、DataTable转到Json格式数据
- 将DataTable或Ilist<>转换成JSON格式
- 将DataTable或Ilist<>转换成JSON格式
- 实体或List(IList<T>)转换JSON格式的简便方法
- DataTable转换为List<T>或者DataRow转换为T
- List<T>集合转换为EasyUI TreeGrid所需要的Json格式通用工具
- C#中怎样将List<自己定义>转为Json格式 及相关函数-DataContractJsonSerializer
- c# Json List<T> DataTable 数据相互转换
- json格式,list<map>,数据库表的关系
- 数组json格式的字符串 转 list<Bean>
- C#中如何将List<自定义>转为Json格式 及相关函数-DataContractJsonSerializer
- java处理List<String>的内容将其转换成标准json格式
- 利用Jquery让返回的各类数据(string、集合(List<>)、类)以Json数据格式返回,为什么要用到result.d (JQuery ajax 返回数据 使用 .d 的问题)
- Asp.net 将DataTable 或者DataSet 转换为Json 格式
- 工具类:解析json格式java bean,List<bean>
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象