Previously written with reflection, conversion, and later felt a big loophole, recently found someone wrote this help class, so save it down
public class Jsonhelper
{
<summary>
DataRow transfer JSON
</summary>
<param name= "Row" >DataRow</param>
<returns>json Format Objects </returns>
public static object Datarowtojson (DataRow row)
{
dictionary<string, object> dataList = new dictionary<string, object> ();
foreach (DataColumn column in row. Table.columns)
{
Datalist.add (column. ColumnName, Row[column]);
}
Return Objecttojson (dataList);
}
//<summary>
//DataRow to Object, generic method
///</summary>
//<typeparam name= "T" > Type </ Typeparam>
//<param name= "Row" >DATAROW</PARAM>
///<returns>json Format Object </returns>
public static T datarowtoobject<t> (DataRow row)
{
return jsontoobject<t> (Datarowtojson (Row). ToString ());
}
//<summary>
//DataRow to Object, generic method
///</summary>
//<typeparam name= "T" > Type </ Typeparam>
//<param name= "table" >DATATABLE</PARAM>
///<returns>json Format Object </returns
public static list<t> datatabletolist<t> (DataTable table)
{
Return jsontolist<t> ( Datatabletojson (table). ToString ());
}
///<summary>
//DataRow to Object, generic method
//</summary>
//<typeparam name= "T" > type < /typeparam>
//<param name= "Jsontext" >json text </param>
//<returns>json Format Object </ Returns>
public static list<t> jsontolist<t> (string jsontext)
{
Return jsontoobject<list <T>> (Jsontext);
}
//<summary>
//object to JSON
//</summary>
//<param name= "obj" > Object </param>
<returns>json formatted string </returns>
public static object Objecttojson (Object obj)
{
Try
{
Jsonserializersettings jset = new Jsonserializersettings ();
Jset. referenceloophandling = Referenceloophandling.ignore;
Jset. Converters.add (new Isodatetimeconverter {DateTimeFormat = "yyyy '/' mm '/' dd ' ' HH ': ' mm ': ' SS '});
return Jsonconvert.serializeobject (obj, jset);
}
catch (Exception ex)
{
throw new Exception ("Jsonhelper.objecttojson ():" + ex. Message);
}
}
//<summary>
//data table to JSON
//</summary>
//<param name= "dataTable" > Data Table </param>
//<returns>json string </returns>
public static object Datatabletojson (DataTable DataTable)
{
return Objecttojson (DataTable);
}
//<summary>
///JSON text-to-object, generic method
//</summary>
//<typeparam name= "T" > Type </typepa Ram>
//<param name= "Jsontext" >json text </param>
//<returns> object of the specified type </returns>
public static T jsontoobject<t> (string jsontext)
{
Try
{
Return jsonconvert.deserializeobject <T> (Jsontext.replace ("undefined", "null"));
}
catch (Exception ex)
{
throw new Exception ("Jsonhelper.jsontoobject ():" + ex. Message);
}
}
<summary>
JSON text to Object
</summary>
<param name= "Jsontext" >json text </param>
<param name= "type" > Type </param>
<returns> objects of the specified type </returns>
public static Object Jsontoobject (string jsontext, type type)
{
Try
{
Return Jsonconvert.deserializeobject (Jsontext.replace ("undefined", "null"), type);
}
catch (Exception ex)
{
throw new Exception ("Jsonhelper.jsontoobject ():" + ex. Message);
}
}
<summary>
[{Column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]
</summary>
<param name= "Strjson" >json string </param>
<returns>DataTable</returns>
public static DataTable jsontodatatable (String Strjson)
{
Return Jsonconvert.deserializeobject (Strjson, typeof (DataTable)) as DataTable;
}
}
JSON Object Cross-transfer