Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Text;
Using System.Reflection;
Using System.Collections;
Using System.Data;
Using System.Data.Common;
<summary>
Summary description for Formattojson
</summary>
public class Formattojson
{
Public Formattojson ()
{
//
Todo:add constructor Logic here
//
}
<summary>
List turns into 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 (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 ();
}
<summary>
List turns into 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, obj. GetType (). Name);
}
<summary>
Convert object to JSON string
</summary>
<param name= "Jsonobject" > Objects </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 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.tostring ());
}
jsonstring + = "\" + ToJson (propertyinfo[i). Name) + "\": "+ Value +", ";
}
Jsonstring.remove (Jsonstring.length-1, jsonstring.length);
return jsonstring + "}";
}
<summary>
Object Collection Conversion JSON
</summary>
<param name= "Array" > Collection objects </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 + "]";
}
<summary>
Normal collection Conversion JSON
</summary>
<param name= "Array" > Collection objects </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 + "]";
}
<summary>
DataTable converted to JSON
</summary>
<param name= "Table" >datatable objects </param>
<returns>json string </returns>
public static string ToJson (DataTable dt)
{
StringBuilder jsonstring = new StringBuilder ();
if (dt. Rows.Count = = 0)
{
Jsonstring.append ("[{}]");
return jsonstring.tostring ();
}
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 turns into 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 (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 ();
}
<summary>
DataReader Convert 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 ();
}
<summary>
DataSet converted to JSON
</summary>
<param name= "DataSet" >dataset objects </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 + "}";
}
<summary>
Filter special characters
</summary>
<param name= "S" ></param>
<returns></returns>
private static string String2json (string s)
{
System.Text.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 static string StringFormat (String str, type type)
{
if (type = = typeof (String))
{
str = String2json (str);
str = "\" "+ str +" \ ";
}
else if (type = = typeof (DateTime))
{
str = "\" "+ convert.todatetime (str). ToShortDateString () + "\" ";
}
else if (type = = typeof (bool))
{
str = str. ToLower ();
}
if (str. Length = = 0)
str = "\" \ "";
return str;
}
}
Class for object to JSON