This C # wrapper class can be used to convert data in various formats into JSON format, including list conversion to JSON, normal set conversion JSON, DataSet to JSON, DataTable to JSON, DataReader to JSON, etc. If you need to convert an object to JSON, you can use this class.
Using system;using system.collections.generic;using system.text;using system.data;using System.Reflection;using System.collections;using System.Data.Common; Namespace dotnet.utilities{//json Conversion class public class Convertjson {#region Private method///// Filter Special characters // 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 (); } //////format character, date type, Boolean ///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 #region List into JSON///// list converted to JSON //public static string Listtojson
(IList
List) {Object obj = list[0]; Return Listtojson
(list, obj.) GetType (). Name); } ///
/// list converted to JSON //public static string Listtojson
(IList
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
(); 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 convert #region object to JSON//
/// object converted to JSON /////
Object///
JSON string
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 are 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.tostr ing ()); } jsonstring + = "\" + ToJson (propertyinfo[i). Name) + "\": "+ Value +", ";} Jsonstring.remove (Jsonstring.length-1, jsonstring.length); return jsonstring + "}"; #endregion #region Object Collection conversion JSON//
/// object Collection Conversion JSON /////
Collection Object///
JSON string
public static string ToJson (IEnumerable array) {string jsonstring = "["; foreach (object item in array) {jsonstring + = ToJson (item) + ",";} jsonstring.remove (Jsonstring.length-1, JSONSTRING.L Ength); return jsonstring + "]"; } #endregion #region Normal collection conversion JSON//
/// General set conversion JSON /////
Collection Object///
JSON string
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 is converted to JSON//
/// DataSet converted to JSON /////
DataSet object///
JSON string
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 convert #region DataTable to JSON//
// DataTable converted to JSON /////
DataTable Object///
JSON string
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 (); } ///
// DataTable converted to JSON //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 convert #region DataReader to JSON//
// DataReader Convert to JSON /////
DataReader Object///
JSON string
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.ge Tname (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}}