C #. NET, JavaScript, and JSON,

Source: Internet
Author: User
Tags object serialization

C #. NET, JavaScript, and JSON,

Preface

If there is no surplus, we will record all the items we use.

1. What is JSON

JSON: JavaScript Object Notation, a lightweight data interaction format, is mainly used for data transmission.

Ii. JSON syntax rules

1. Data is represented by key-value pairs (ing), and is represented;

Example: "name": "Zhao da ".

2. Separate data with commas;

Example: "name": "Zhao da", "age": "27 ".

3. The set (object) of data ing is included in;

Example: A student data object:

{"Id": "1", "name": "Zhao da", "age": "27", "gender": "male "}

4. The set of parallel data (in the form of an array of objects) is included in "[]" and separated;

Example: an array of two student data objects:

[{"Id": "1", "name": "Zhao da", "age": "27", "gender": "male" },{ "id ": "2", "name": "Qian 'er", "age": "27", "gender": "male"}]

Iii. Data Types in JSON data

  • Number (integer or floating point number)
  • String (in double quotation marks)
  • Boolean (True or False)
  • Array (in)
  • Object (in)
  • NULL

Iv. Data presentation in JSON format

  According to the JSON syntax rules, JSON data generally has the following forms:

1. single object;

2. object set (array );

3. Combinations of 1 and 2: nesting;

4. Pure string;

V. C #. NET and JSON

  In the C #. NET environment, there are components that support JSON serialization and deserialization, as well as third-party components,

. NET supports the JavaScriptSerializer class and DataContractJsonSerializer class,

Third parties support Json.net (newtonsoft. json) and so on.

  1. Use the JavaScriptSerializer class to serialize and deserialize JSON data:

First, you must reference System. Web. Extentions. dll in reference.

Class file reference: using System. Web. Script. Serialization;

  
1 Dictionary <string, string> stu = new Dictionary <string, string> (); 2 stu. add ("id", "1"); 3 stu. add ("name", "Zhao da"); 4 stu. add ("age", "27"); 5 stu. add ("gender", "male"); 6 7 JavaScriptSerializer js = new JavaScriptSerializer (); 8 string stuJson = js. serialize (stu); 9 Console. writeLine ("Use the JavaScriptSerializer class to serialize data in JSON format:"); 10 Console. writeLine (stuJson );
Example: JSON Single Object serialization
1 string deJson = stuJson; 2 Dictionary <string, string> deStu = js. deserialize <Dictionary <string, string> (deJson); 3 Console. writeLine ("deserialization of JSON format data using the JavaScriptSerializer class specified class:"); 4 foreach (string s in stu. keys) 5 {6 Console. writeLine (s. toString () + "=" + stu [s]. toString (); 7} 8 Console. readLine ();
Example: JSON single object deserialization

Running result:

  

  2. Use the third-party Json.net (newtonsoft. json) class library to serialize and deserialize JSON data:

Reference: http://www.newtonsoft.com/json

Vi. JavaScript and JSON

  1. Customize and parse JSON strings in JavaScript;

Example: Student Zhao da information:

    
Function myJson () {var stu = {"id": "1", "name": "Zhao da", "age": "27", "gender ": "male", "school": {"class": "03", "teacher": "Zhao's teacher" }}; alert ("id =" + stu. id + "," + "name =" + stu. name + "," + "age =" + stu. age + "," + "gender =" + stu. gender );}
Custom JSON string

:

    

After the custom JSON string is displayed, the stu object can be automatically recognized as a member variable.

  2. JSON data imported from the background is parsed and converted to a JSON data object:

Example: Student Zhao da information (Ajax requests are processed using JQuery ):

    
 1         $(document).ready(function () { 2             $.ajax({ 3                 url: "AjaxWeb.ashx", 4                 type: "GET", 5                 dataType: "JSON", 6                 success: function (data) { 7                     var stu = JSON.parse(data); 8                     alert("id=" + stu.id + ", " + "name=" + stu.name + ", " + "age=" + stu.age + ", " + "gender=" + stu.gender + ", " + "teacher=" + stu.school["teacher"]); 9                 },10                 error: function (e) {11                     alert(e.toString());12                 }13             })14         })
Obtain the JSON string from the background and convert it to a JSON object.
1 public void ProcessRequest (HttpContext context) 2 {3 context. response. write (myJson2 (); 4} 5 6 private string myJson2 () 7 {8 string json = "{\" id \ ": \" 1 \", \ "name \": \ "Zhao da \", \ "age \": \ "27 \", \ "gender \": \ "male \", \ "school \": {\ "class \": \ "03 \", \ "teacher \": \ "Zhao DA's teacher \"}}"; 9. JavaScriptSerializer js = new JavaScriptSerializer (); 10 json = js. serialize (json); 11 return json; 12}
Background File Processing code

:

  

Debugging of F12 Developer Tools in Chrome:

  

VII. References

  • Baidu encyclopedia
  • Baidu search
  • MOOC (http://www.imooc.com /)
  • JSON online verification and formatting (http://jsonlint.com /)

Related Article

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.