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 );
}
}