Parsing JSON in. Net: datacontractjsonserializer class

Source: Internet
Author: User

JSON is a lightweight text transmission format, which is generally used for front-end and back-end data exchange.

Datacontractjsonserializer is a specialized class of fm3.5 for JSON serialization and deserialization. The following is a simple application of datacontractjsonserializer.

 

Assume that the client has a JSON object, which is uploaded to the background through Ajax and parsed in the background.

 

The Code is as follows:

VaR people = [{"username": "T1", "password": "111111", "sex": "male" },{ "username": "T2 ", "password": "222222", "sex": "female"}]; <br/> var url = "default. aspx? People = "+ escape (people. tojsonstring (); <br/> request. open ("Post", URL, true); <br/> request. onreadystatechange = updatepage; <br/> request. send (null); <br/>

If the tojsonstring function runs normally, JSON. JS is referenced.

<SCRIPT src = "JSON. js" type = "text/JavaScript"> </SCRIPT>

 

Background code:

Create a test class testobj (the properties in testobj are consistent with those passed by the client)

[Datacontract] // serialization <br/> public class testobj <br/> {<br/> [datamember] <br/> Public String username {Get; set ;} <br/> [datamember] <br/> Public String password {Get; Set ;}< br/> [datamember] <br/> Public String sex {Get; set ;} </P> <p> Public testobj (string U, string P, string s) <br/>{< br/> username = u; <br/> Password = P; <br/> sex = s; <br/>}< br/>

New serialization and deserialization Methods

// JSON serialization </P> <p> Public static string tojsjson (Object item) <br/>{< br/> datacontractjsonserializer serializer = new datacontractjsonserializer (item. getType (); <br/> using (memorystream MS = new memorystream () <br/>{< br/> serializer. writeobject (MS, item); <br/> stringbuilder sb = new stringbuilder (); <br/> Sb. append (encoding. utf8.getstring (Ms. toarray (); <br/> return sb. tostring (); <br/>}</P> <p> // deserialization </P> <p> Public static t fromjsonto <t> (string jsonstring) <br/>{< br/> datacontractjsonserializer SER = new datacontractjsonserializer (typeof (t); <br/> using (memorystream MS = new memorystream (encoding. utf8.getbytes (jsonstring) <br/>{< br/> T jsonobject = (t) Ser. readobject (MS); <br/> return jsonobject; <br/>}< br/>}

 

Call:

Deserialization JSON (resolution) Note: people is passed by the client.

// Obtain the JSON string <br/> string jsonstr = request ["people"]; <br/> List <testobj> OBJ = JSON. fromjsonto <list <testobj> (jsonstr); <br/> foreach (testobj item in OBJ) <br/>{< br/> response. write (string. format ("username: {0}, password: {1}, sex: {2}/R/N", item. username, item. password, item. sex); <br/>}< br/> response. end ();

 

Serialized into a JSON string

List <testobj> Users = new list <testobj> (); <br/> users. add (New testobj ("T1", "1", "male"); <br/> users. add (New testobj ("T2", "2", "female"); <br/> string JSON = JSON. tojsjson (users); <br/> response. write (JSON); <br/> response. end ();

 

The foreground retrieves the JSON string returned by the background:

 Function updatepage () {<br/> If (request. readystate = 4) {<br/> If (request. status = 200) {<br/> var response = request. responsetext; <br/> // convert to object <br/> // method 1 <br/> response = response. parsejson (); <br/> // method 2 <br/> // response = eval ("+ response + ")"); <br/> // object access method <br/> document. getelementbyid ("d1 "). innerhtml = response [1]. sex; <br/> // directly output <br/> // document. getelementbyid ("d1 "). innerhtml = response; <br/>}< br/>

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.