SoFunction
Updated on 2025-03-06

C# implements any data type to output in json format


/// Convert List to json  
/// </summary> 
/// <typeparam name="T"></typeparam> 
/// <param name="jsonName"></param> 
/// <param name="list"></param> 
/// <returns></returns> 
public static string ListToJson<T>(IList<T> list, string jsonName) 

 StringBuilder Json = new StringBuilder(); 
 if ((jsonName)) 
  jsonName = list[0].GetType().Name; 
 ("{/"" + jsonName + "/":["); 
 if ( > 0) 
 { 
  for (int i = 0; i < ; i++) 
  { 
   T obj = <T>(); 
   PropertyInfo[] pi = ().GetProperties(); 
   ("{"); 
   for (int j = 0; j < ; j++) 
   { 
    Type type = pi[j].GetValue(list[i], null).GetType(); 
    ("/"" + pi[j].() + "/":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type)); 

    if (j < - 1) 
    { 
     (","); 
    } 
   } 
   ("}"); 
   if (i < - 1) 
   { 
    (","); 
   } 
  } 
 } 
 ("]}"); 
 return (); 


/// <summary> 
/// Convert List to json
/// </summary> 
/// <typeparam name="T"></typeparam> 
/// <param name="list"></param> 
/// <returns></returns> 
public static string ListToJson<T>(IList<T> list) 

 object obj = list[0]; 
 return ListToJson<T>(list, ().Name); 


/// <summary>  
/// Convert object to Json string
/// </summary>  
/// <param name="jsonObject">Object</param>
/// <returns>Json string</returns>
public static string ToJson(object jsonObject) 

 string jsonString = "{"; 
 PropertyInfo[] propertyInfo = ().GetProperties(); 
 for (int i = 0; i < ; i++) 
 { 
  object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null); 
  string value = ; 
  if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan) 
  { 
   value = "'" + () + "'"; 
  } 
  else if (objectValue is string) 
  { 
   value = "'" + ToJson(()) + "'"; 
  } 
  else if (objectValue is IEnumerable) 
  { 
   value = ToJson((IEnumerable)objectValue); 
  } 
  else
  { 
   value = ToJson(()); 
  } 
  jsonString += "/"" + ToJson(propertyInfo[i].Name) + "/":" + value + ","; 
 } 
 ( - 1, ); 
 return jsonString + "}"; 


/// <summary>  
/// Object collection conversion Json
/// </summary>  
/// <param name="array">collection object</param>
/// <returns>Json string</returns>
public static string ToJson(IEnumerable array) 

 string jsonString = "["; 
 foreach (object item in array) 
 { 
  jsonString += ToJson(item) + ","; 
 } 
 ( - 1, ); 
 return jsonString + "]"; 


/// <summary>  
/// Normal collection conversion Json  
/// </summary>  
/// <param name="array">collection object</param>
/// <returns>Json string</returns>
public static string ToArrayString(IEnumerable array) 

 string jsonString = "["; 
 foreach (object item in array) 
 { 
  jsonString = ToJson(()) + ","; 
 } 
 ( - 1, ); 
 return jsonString + "]"; 


/// <summary>  
/// Convert Datatable to Json
/// </summary>  
/// <param name="table">Datatable object</param>
/// <returns>Json string</returns>
public static string ToJson(DataTable dt) 

 StringBuilder jsonString = new StringBuilder(); 
 ("["); 
 DataRowCollection drc = ; 
 for (int i = 0; i < ; i++) 
 { 
  ("{"); 
  for (int j = 0; j < ; j++) 
  { 
   string strKey = [j].ColumnName; 
   string strValue = drc[i][j].ToString(); 
   Type type = [j].DataType; 
   ("/"" + strKey + "/":"); 
   strValue = StringFormat(strValue, type); 
   if (j < - 1) 
   { 
    (strValue + ","); 
   } 
   else
   { 
    (strValue); 
   } 
  } 
  ("},"); 
 } 
 ( - 1, 1); 
 ("]"); 
 return (); 


/// <summary> 
/// Convert DataTable to Json
/// </summary> 
/// <param name="jsonName"></param> 
/// <param name="dt"></param> 
/// <returns></returns> 
public static string ToJson(DataTable dt, string jsonName) 

 StringBuilder Json = new StringBuilder(); 
 if ((jsonName)) 
  jsonName = ; 
 ("{/"" + jsonName + "/":["); 
 if ( > 0) 
 { 
  for (int i = 0; i < ; i++) 
  { 
   ("{"); 
   for (int j = 0; j < ; j++) 
   { 
    Type type = [i][j].GetType(); 
    ("/"" + [j].() + "/":" + StringFormat([i][j].ToString(), type)); 
    if (j < - 1) 
    { 
     (","); 
    } 
   } 
   ("}"); 
   if (i < - 1) 
   { 
    (","); 
   } 
  } 
 } 
 ("]}"); 
 return (); 


/// <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(); 
 ("["); 
 while (()) 
 { 
  ("{"); 
  for (int i = 0; i < ; i++) 
  { 
   Type type = (i); 
   string strKey = (i); 
   string strValue = dataReader[i].ToString(); 
   ("/"" + strKey + "/":"); 
   strValue = StringFormat(strValue, type); 
   if (i < - 1) 
   { 
    (strValue + ","); 
   } 
   else
   { 
    (strValue); 
   } 
  } 
  ("},"); 
 } 
 (); 
 ( - 1, 1); 
 ("]"); 
 return (); 


/// <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 ) 
 { 
  jsonString += "/"" + + "/":" + ToJson(table) + ","; 
 } 
 jsonString = (','); 
 return jsonString + "}"; 


/// <summary> 
/// Filter special characters
/// </summary> 
/// <param name="s"></param> 
/// <returns></returns> 
private static string String2Json(String s) 

 StringBuilder sb = new StringBuilder(); 
 for (int i = 0; i < ; i++) 
 { 
  char c = ()[i]; 
  switch (c) 
  { 
   case '/"': 
    ("///""); break; 
   case '//': 
    ("////"); break; 
   case '/': 
    ("///"); break; 
   case '/b': 
    ("//b"); break; 
   case '/f': 
    ("//f"); break; 
   case '/n': 
    ("//n"); break; 
   case '/r': 
    ("//r"); break; 
   case '/t': 
    ("//t"); break; 
   default: 
    (c); break; 
  } 
 } 
 return (); 


/// <summary> 
/// Format character type, date type, boolean type
/// </summary> 
/// <param name="str"></param> 
/// <param name="type"></param> 
/// <returns></returns> 
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 = (); 
 } 
 return str; 
}