C # jsonhelper

Source: Internet
Author: User

In C # projects, JSON strings and object data types are sometimes used for conversion. The following is a conversion tool class:

Note: when an object is generated based on a JSON string, it is assigned a value based on the corresponding attribute name. If the value is greater than or less than the value, no error is reported.

To use the datacontractjsonserializer class, you must:

1. reference the assembly system. runtime. serialization and system. servicemodel. Web.

2. Import the namespace system. runtime. serialization and system. runtime. serialization. JSON

To use the "javascriptserializer" class, you must:

1. Reference assembly system. Web. Extension

2. Import the namespace system. Web. Script. serialization

View code

/// <Summary>
/// JSON serialization and deserialization helper class
/// </Summary>
Public class jsonhelper
{
/// <Summary>
/// JSON serialization
/// </Summary>
Public static string jsonserializer <t> (t obj ){
String jsonstring = string. empty;
Try
{
Datacontractjsonserializer serializer = new datacontractjsonserializer (typeof (t ));

Using (memorystream MS = new memorystream ())
{
Serializer. writeobject (MS, OBJ );
Jsonstring = encoding. utf8.getstring (Ms. toarray ());
}
}
Catch
{
Jsonstring = string. empty;
}
Return jsonstring;
}

/// <Summary>
/// JSON deserialization
/// </Summary>
Public static t jsondeserialize <t> (string jsonstring)
{
T OBJ = activator. createinstance <t> ();
Try
{
Using (memorystream MS = new memorystream (encoding. utf8.getbytes (jsonstring )))
{
Datacontractjsonserializer SER = new datacontractjsonserializer (obj. GetType (); // typeof (t)
T jsonobject = (t) SER. readobject (MS );
Ms. Close ();

Return jsonobject;
}
}
Catch
{
Return default (t );
}
}

// Serialize the datatable into a JSON string
Public static string datatabletojson (datatable DT)
{
If (Dt = NULL | DT. Rows. Count = 0)
{
Return "\"\"";
}
Javascriptserializer myjson = new javascriptserializer ();

List <dictionary <string, Object> List = new list <dictionary <string, Object> ();

Foreach (datarow DR in DT. Rows)
{
Dictionary <string, Object> result = new dictionary <string, Object> ();
Foreach (datacolumn DC in DT. columns)
{
Result. Add (DC. columnname, Dr [DC]. tostring ());
}
List. Add (result );
}
Return myjson. serialize (list );
}

// Serialize the object into a JSON string
Public static string objecttojson (Object OBJ)
{
If (OBJ = NULL)
{
Return string. empty;
}
Javascriptserializer myjson = new javascriptserializer ();

Return myjson. serialize (OBJ );
}

// Deserializes a json string into an object
Public static object jsontoobject (string JSON)
{
If (string. isnullorempty (JSON ))
{
Return NULL;
}
Javascriptserializer myjson = new javascriptserializer ();

Return myjson. deserializeobject (JSON );
}

// Deserializes a json string into an object
Public static t jsontoobject <t> (string JSON)
{
If (string. isnullorempty (JSON ))
{
Return default (t );
}
Javascriptserializer myjson = new javascriptserializer ();

Return myjson. deserialize <t> (JSON );
}
}

 

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.