datatable与json的转化

1将DataTable或Ilist<>转换成JSON格式

using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Reflection;

///
/// 将DataTable或Ilist<>转换成JSON格式
///

public class ToJson
{
public ToJson()
{

}

//DataTable转成Json
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();
}

//List转成json
public static string ObjectToJson(string jsonName, IList IL)
{
StringBuilder Json = new StringBuilder();
Json.Append("{\"" + jsonName + "\":[");
if (IL.Count > 0)
{
for (int i = 0; i < IL.Count; i++)
{
T obj = Activator.CreateInstance();
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();
}
}
2各种数据集转换为json///
/// 对象转换为Json字符串
///

/// 对象
/// Json字符串
public static string ToJson(object jsonObject)
{
string jsonString = "{";
PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
for (int i = 0; i < propertyInfo.Length; i++)
{
object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
string value = string.Empty;
if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
{
value = "'" + objectValue.ToString() + "'";
}
else if(objectValue is string)
{
value = "'" + ToJson(objectValue.ToString()) + "'";
}
else if (objectValue is IEnumerable)
{
value = ToJson((IEnumerable)objectValue);
}
else
{
value = ToJson(objectValue.ToString());
}
jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ",";
}
return Json.DeleteLast(jsonString) + "}";
}
///
/// 对象集合转换Json
///

/// 集合对象
/// Json字符串
public static string ToJson(IEnumerable array)
{
string jsonString = "[";
foreach (object item in array)
{
jsonString += Json.ToJson(item) + ",";
}
return Json.DeleteLast(jsonString) + "]";
}
///
/// 普通集合转换Json
///

/// 集合对象
/// Json字符串
public static string ToArrayString(IEnumerable array)
{
string jsonString = "[";
foreach (object item in array)
{
jsonString = ToJson(item.ToString()) + ",";
}
return Json.DeleteLast(jsonString) + "]";
}
///
/// 删除结尾字符
///

/// 需要删除的字符
/// 完成后的字符串
private static string DeleteLast(string str)
{
if (str.Length > 1)
{
return str.Substring(0, str.Length - 1);
}
return str;
}
///
/// Datatable转换为Json
///

/// Datatable对象
/// Json字符串
public static string ToJson(DataTable table)
{
string jsonString = "[";
DataRowCollection drc = table.Rows;
for (int i = 0; i < drc.Count; i++)
{
jsonString += "{";
foreach (DataColumn column in table.Columns)
{
jsonString += "\"" + ToJson(column.ColumnName) + "\":";
if (column.DataType == typeof(DateTime) || column.DataType == typeof(string))
{
jsonString += "\"" + ToJson(drc[i][column.ColumnName].ToString()) + "\",";
}
else
{
jsonString += ToJson(drc[i][column.ColumnName].ToString()) + ",";
}
}
jsonString = DeleteLast(jsonString) + "},";
}
return DeleteLast(jsonString) + "]";
}
///
/// DataReader转换为Json
///

/// DataReader对象
/// Json字符串
public static string ToJson(DbDataReader dataReader)
{
string jsonString = "[";
while (dataReader.Read())
{
jsonString += "{";

for (int i = 0; i < dataReader.FieldCount; i++)
{
jsonString += "\"" + ToJson(dataReader.GetName(i)) + "\":";
if (dataReader.GetFieldType(i) == typeof(DateTime) || dataReader.GetFieldType(i) == typeof(string))
{
jsonString += "\"" + ToJson(dataReader[i].ToString()) + "\",";
}
else
{
jsonString += ToJson(dataReader[i].ToString()) + ",";
}
}
jsonString = DeleteLast(jsonString) + "}";
}
dataReader.Close();
return DeleteLast(jsonString) + "]";
}
///
/// DataSet转换为Json
///

/// DataSet对象
/// Json字符串
public static string ToJson(DataSet dataSet)
{
string jsonString = "{";
foreach (DataTable table in dataSet.Tables)
{
jsonString += "\"" + ToJson(table.TableName) + "\":" + ToJson(table) + ",";
}
return jsonString = DeleteLast(jsonString) + "}";
}
///
/// String转换为Json
///

/// String对象
/// Json字符串
public static string ToJson(string value)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}

string temstr;
temstr = value;
temstr = temstr.Replace("{", "{").Replace("}", "}").Replace(":", ":").Replace(",", ",").Replace("[", "【").Replace("]", "】").Replace(";", ";").Replace("\n", "
").Replace("\r", "");

temstr = temstr.Replace("\t", " ");
temstr = temstr.Replace("'", "\'");
temstr = temstr.Replace(@"\", @"\\");
temstr = temstr.Replace("\"", "\"\"");
return temstr;
}

你可能感兴趣的:(ASP.NET,json,J#)