Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data;
Using System. Reflection;
Using System. Collections;
Using System. Data. Common;
Public class ConvertJson
{
# Region private Method
/// <Summary>
/// Filter special characters
/// </Summary>
Private static string String2Json (String s)
{
StringBuilder sb = new StringBuilder ();
For (int I = 0; I <s. Length; I ++)
{
Char c = s. ToCharArray () [I];
Switch (c)
{
Case '\"':
Sb. Append ("\" "); break;
Case '\\':
Sb. Append ("\\\\"); break;
Case '/':
Sb. Append ("\/"); break;
Case '\ B ':
Sb. Append ("\ B"); break;
Case '\ F ':
Sb. Append ("\ f"); break;
Case '\ N ':
Sb. Append ("\ n"); break;
Case '\ R ':
Sb. Append ("\ r"); break;
Case '\ t ':
Sb. Append ("\ t"); break;
Default:
Sb. Append (c); break;
}
}
Return sb. ToString ();
}
/// <Summary>
/// Format String, date, and Boolean
/// </Summary>
Private static string StringFormat (string str, Type type)
{
If (type = typeof (string ))
{
Str = String2Json (str );
Str = "\" "+ str + "\"";
}
Else if (type = typeof (DateTime ))
{
Str = "\" "+ str + "\"";
}
Else if (type = typeof (bool ))
{
Str = str. ToLower ();
}
Else if (type! = Typeof (string) & string. IsNullOrEmpty (str ))
{
Str = "\" "+ str + "\"";
}
Return str;
}
# Endregion
# Convert region List to Json
/// <Summary>
/// Convert List to Json
/// </Summary>
Public static string ListToJson <T> (IList <T> list)
{
Object obj = list [0];
Return ListToJson <T> (list, obj. GetType (). Name );
}
/// <Summary>
/// Convert List to Json
/// </Summary>
Public static string ListToJson <T> (IList <T> list, string jsonName)
{
StringBuilder Json = new StringBuilder ();
If (string. IsNullOrEmpty (jsonName) jsonName = list [0]. GetType (). Name;
Json. Append ("{\" "+ jsonName + "\":[");
If (list. Count> 0)
{
For (int I = 0; I <list. Count; I ++)
{
T obj = Activator. CreateInstance <T> ();
PropertyInfo [] pi = obj. GetType (). GetProperties ();
Json. Append ("{");
For (int j = 0; j <pi. Length; j ++)
{
Type type = pi [j]. GetValue (list [I], null). GetType ();
Json. append ("\" "+ pi [j]. name. toString () + "\": "+ StringFormat (pi [j]. getValue (list [I], null ). toString (), type ));
If (j <pi. Length-1)
{
Json. Append (",");
}
}
Json. Append ("}");
If (I <list. Count-1)
{
Json. Append (",");
}
}
}
Json. Append ("]}");
Return Json. ToString ();
}
# Endregion
# Converting region object to Json
/// <Summary>
/// Convert the object to Json
/// </Summary>
/// <Param name = "jsonObject"> Object </param>
/// <Returns> Json string </returns>
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 + ",";
}
JsonString. Remove (jsonString. Length-1, jsonString. Length );
Return jsonString + "}";
}
# Endregion
# Region object set conversion Json
/// <Summary>
/// Convert the object set to Json
/// </Summary>
/// <Param name = "array"> set object </param>
/// <Returns> Json string </returns>
Public static string ToJson (IEnumerable array)
{
String jsonString = "[";
Foreach (object item in array)
{
JsonString + = ToJson (item) + ",";
}
JsonString. Remove (jsonString. Length-1, jsonString. Length );
Return jsonString + "]";
}
# Endregion
# Region normal set conversion Json
/// <Summary>
/// Convert a common set to Json
/// </Summary>
/// <Param name = "array"> set object </param>
/// <Returns> Json string </returns>
Public static string ToArrayString (IEnumerable array)
{
String jsonString = "[";
Foreach (object item in array)
{
JsonString = ToJson (item. ToString () + ",";
}
JsonString. Remove (jsonString. Length-1, jsonString. Length );
Return jsonString + "]";
}
# Endregion
# Region DataSet to Json
/// <Summary>
/// Convert DataSet to Json
/// </Summary>
/// <Param name = "dataSet"> DataSet object </param>
/// <Returns> Json string </returns>
Public static string ToJson (DataSet dataSet)
{
String jsonString = "{";
Foreach (DataTable table in dataSet. Tables)
{
JsonString + = "\" "+ table. TableName +" \ ":" + ToJson (table) + ",";
}
JsonString = jsonString. TrimEnd (',');
Return jsonString + "}";
}
# Endregion
# Region Datatable converted to Json
/// <Summary>
/// Datatable converted to Json
/// </Summary>
/// <Param name = "table"> Datatable object </param>
/// <Returns> Json string </returns>
Public static 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 = StringFormat (strValue, type );
If (j <dt. Columns. Count-1)
{
JsonString. Append (strValue + ",");
}
Else
{
JsonString. Append (strValue );
}
}
JsonString. Append ("},");
}
JsonString. Remove (jsonString. Length-1, 1 );
JsonString. Append ("]");
Return jsonString. ToString ();
}
/// <Summary>
/// DataTable converted to Json
/// </Summary>
Public static string ToJson (DataTable dt, string jsonName)
{
StringBuilder Json = new StringBuilder ();
If (string. IsNullOrEmpty (jsonName) jsonName = dt. TableName;
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 ++)
{
Type type = dt. Rows [I] [j]. GetType ();
Json. append ("\" "+ dt. columns [j]. columnName. toString () + "\": "+ StringFormat (dt. rows [I] [j]. toString (), type ));
If (j <dt. Columns. Count-1)
{
Json. Append (",");
}
}
Json. Append ("}");
If (I <dt. Rows. Count-1)
{
Json. Append (",");
}
}
}
Json. Append ("]}");
Return Json. ToString ();
}
# Endregion
# Region DataReader to Json
/// <Summary>
/// Convert DataReader to Json
/// </Summary>
/// <Param name = "dataReader"> DataReader object </param>
/// <Returns> Json string </returns>
Public static string ToJson (DbDataReader dataReader)
{
StringBuilder jsonString = new StringBuilder ();
JsonString. Append ("[");
While (dataReader. Read ())
{
JsonString. Append ("{");
For (int I = 0; I <dataReader. FieldCount; I ++)
{
Type type = dataReader. GetFieldType (I );
String strKey = dataReader. GetName (I );
String strValue = dataReader [I]. ToString ();
JsonString. Append ("\" "+ strKey + "\":");
StrValue = StringFormat (strValue, type );
If (I <dataReader. FieldCount-1)
{
JsonString. Append (strValue + ",");
}
Else
{
JsonString. Append (strValue );
}
}
JsonString. Append ("},");
}
DataReader. Close ();
JsonString. Remove (jsonString. Length-1, 1 );
JsonString. Append ("]");
Return jsonString. ToString ();
}
# Endregion
}