C # objects, strings, dataTable, DataReader, datasets, object collections are converted to JSON string methods.
public class Convertjson {#region Private method///<summary>///filter special characters///</summary> <param name= "S" > String </param>//<returns>json string </returns> private Stati C 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 character, date, Boolean///</summary>//<param name= "str" >< /param>//<param name= "type" ></param>///<returns></returns> Private St Atic 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///<summary>//List to JSON//</SUMMARY&G T <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, obj. GetType (). Name); }//<summary>//list converted to JSON///</summary>//<typeparam name= "T1" >< /typeparam>//<param name= "list" ></param>///<param name= "P" ></param>/ <returns></returns> private 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 (); Convert #endregion #region object to JSON///<summary>//object to JSON///</summary> <param name= "Jsonobject" >json objects </param>///<returns>json Strings </returns> Pub Lic 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) obje Ctvalue); } else {value = ToJson (objectvalue.tostring ()); } jsonstring + = "\" + ToJson (propertyinfo[i). Name) + "\": "+ Value +", "; } jsonstring.remove (Jsonstring.length-1, jsonstring.length); return jsonstring + "}"; Convert #endregion #region Object collection to JSON///<summary>//object collection to JSON//</SUMMARY&G T <param name= "Array" > Object collection </param>//<returns>json string </returns> public static St Ring 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 collection conversion JSON///<summary>//General collection conversion JSON//</summ ary>//<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 (item. ToString ()) + ","; } jsonstring.remove (Jsonstring.length-1, jsonstring.length); return jsonstring + "]"; } #endregion #region DataSet to JSON///<summary>//DataSet to JSON// </summary>//<param name= "DataSet" >dataset object </param>//<returns>json string &L T;/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 convert #region DataTable to JSON///<summary>//DataTable to JSON </summary>//<param name= "table" >datatable objects </param>//<returns>j Son 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 str ing 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//<summary>//DataReader to JSON </summary>//<param name= "DataReader" >datareader objects </param>//<ret Urns>json string </returns> public static string ToJson (DbDataReader dataReader) {Stringbu Ilder 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}
Datasets, strings, DataTable, and objects in C # are converted to JSON