C # Implementation of JSON serialization and deserialization

Source: Internet
Author: User

1. Using the JavaScriptSerializer class for serializationnamespace:System.Web.Script.Serializationeg:
1 //Serialization of2 Private stringObjecttojson (person obj)3 {4JavaScriptSerializer jsonserialize =NewJavaScriptSerializer ();5     returnjsonserialize.serialize (obj);6 }7 //deserialization8 PrivatePerson Jsontoobject (stringjsonstr)9 {TenJavaScriptSerializer jsonserialize =NewJavaScriptSerializer (); One     returnJsonserialize.deserialize<person>(JSONSTR); A}

Note: Non-serialized properties can be marked with [Scriptignore]

2. Using the DataContractJsonSerializer class for serialization

Namespace:System.Runtime.Serialization.Json

eg

1 //Serialization of2 using(MemoryStream stream =NewMemoryStream ())3 {4DataContractJsonSerializer jsonserialize =NewDataContractJsonSerializer (person);5 Jsonserialize.writeobject (stream, obj);6Jsonstr =Encoding.UTF8.GetString (stream. ToArray ());7 }8 //deserialization9 using(MemoryStream stream =NewMemoryStream (Encoding.UTF8.GetBytes (JSONSTR)))Ten { OneDataContractJsonSerializer jsonserialize =NewDataContractJsonSerializer (person); Aobj =(person) jsonserialize.readobject (stream); -}

Note: Non -serialized properties canbe marked with [Ignoredatamember]

C # Implementation of 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.