JSON Object Cross-transfer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.