One
The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize data passed between the browser and the WEB server. You cannot access this instance of the serializer. However, this class exposes the public API. Therefore, you can use this class when you want to use JavaScript object Symbols (JSON) in managed code.
To serialize an object, use the Serialize method. To deserialize a JSON string, use the deserialize or Deserializeobject method. To serialize and deserialize types that are not natively supported by the JavaScriptSerializer, use the JavaScriptConverter class to implement a custom converter. Then, use the Registerconverters method to register the converter.
Mappings between managed types and JSON
The following table shows the mappings between managed types and JSON in the serialization process. These managed types are supported by the JavaScriptSerializer itself. The same mapping is used when deserializing a JSON string into a managed type. However, deserialization may be asymmetric, and not all serializable managed types can be deserialized from JSON.
Two
Implemented through JavaScriptSerializer. Its name space is: System.Web.Script.Serialization
If you want to use it, you must also add
System.Web.Extensions Library File Reference
Reference entity classes: Customer
public class Customer
{
public int Unid {get; set;}
public string CustomerName {get; set;}
}
Class JavaScriptSerializer Description: Provides serialization and deserialization capabilities for Afax-enabled applications.
(i) serialization
Method: public string Serialize (object obj), used to convert an object to a JSON string
public string Scriptserialize (Customer customer)
{
JavaScriptSerializer js = new JavaScriptSerializer ();
Return JS. Serialize (customer);
}
Test:
Customer CC = new Customer {Unid = 1, CustomerName = "John"};
String Strjson = Scriptserialize (cc);
Console.WriteLine (Strjson);
(ii) deserialization
Public Customer scriptdeserialize (string Strjson)
{
JavaScriptSerializer js = new JavaScriptSerializer ();
Return JS. Deserialize<customer> (Strjson);
}
Implemented by the Deserialize<t> method.
Test:
Customer C1 = scriptdeserialize (Strjson);
Console.WriteLine (C1. Unid + "" + C1. CustomerName);
(c) Method generics
public string scriptserialize<t> (T t)
{
JavaScriptSerializer js = new JavaScriptSerializer ();
Return JS. Serialize (t);
}
Public T scriptdeserialize<t> (string Strjson)
{
JavaScriptSerializer js = new JavaScriptSerializer ();
Return JS. Deserialize<t> (Strjson);
}
Test:
Customer CC = new Customer {Unid = 1, CustomerName = "John"};
String Strjson = Scriptserialize<customer> (cc);
Console.WriteLine (Strjson);
Customer C1 = scriptdeserialize<customer> (Strjson);
Console.WriteLine (C1. Unid + "" + C1. CustomerName);
Turn:
Yyixin column JSON serialization and deserialization--javascriptserializer implementation
--javascriptserializer implementation of C # JSON serialization and deserialization