JSON object conversion, JSON object
I used reflection and conversion before. Later I thought there was a major vulnerability. Recently I found someone wrote this help class, so I saved it.
Public class JSONHelper
{
/// <Summary>
/// Convert DataRow to JSON
/// </Summary>
/// <Param name = "row"> DataRow </param>
/// <Returns> JSON format object </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>
/// Convert 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>
/// Convert 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>
/// Convert 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>
/// Convert the object to JSON
/// </Summary>
/// <Param name = "obj"> Object </param>
/// <Returns> JSON 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>
/// Convert the 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>
/// Convert JSON text to object, generic Method
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "jsonText"> JSON text </param>
/// <Returns> specified object 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>
/// Convert JSON text to an object
/// </Summary>
/// <Param name = "jsonText"> JSON text </param>
/// <Param name = "type"> type </param>
/// <Returns> specified object 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;
}
}