--javascriptserializer implementation of C # JSON serialization and deserialization

Source: Internet
Author: User

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

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.