Simple introduction to C#.net, JavaScript, and JSON (graphic)

Source: Internet
Author: User
Tags object serialization
This article introduced the c#.net, http://www.php.cn/wiki/48.html "target=" _blank ">javascript and JSON related knowledge, has a very good reference value, followed by small series together to see it

Write in front

All hands, no superfluous words, all the dry goods, basically used by me to record.

First, what is JSON

Json:javascript Object Notation is a lightweight data interchange format that is used primarily for data transfer.

Second, the JSON syntax rules

1. The data is represented by a key-value pair (mapping), using ":";

Example: "Name": "Zhao da".

2. Use "," separation between data;

Example: "Name": "Zhao Da", "Age": "27".

3. The collection (object) of the data map is included with "{}";

Example: A student data object:

{"id": "1", "name": "Zhao Da", "Age": "Three", "gender": "Male"}

4. A set of data (an array of objects) is included with "[]", separated by ",";

Example: Two student data object arrays:

[{"id": "1", "name": "Zhao Da", "Age": "Five", "gender": "Male"}, {"id": "2", "Name": "Money Two", "Age": "Three", "gender": "Male"}]

Iii. data types in JSON data

    • Number (integer or floating point)

    • String (in double quotes)

    • Boolean value (True or FALSE)

    • Array (in [])

    • Object (in {})

    • Null

Iv. data representation of JSON format data

The JSON syntax rules show that JSON data generally has the following forms:

1, single object;

2. Collection of objects (arrays);

Combination of 3, 1 and 2: nested;

4, pure string;

V. C #. NET and JSON

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

. NET supports the JavaScriptSerializer class and the DataContractJsonSerializer class,

Third-party support has json.net (Newtonsoft.json) and so on.

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

First you have to quote in the reference: System.Web.Extentions.dll

Referenced in the class file: using System.Web.Script.Serialization;

dictionary<string, string> stu = new dictionary<string, string> ();      Stu. ADD ("id", "1");      Stu. ADD ("name", "Zhao Da");      Stu. ADD ("Age", "+");      Stu. ADD ("Gender", "male");      JavaScriptSerializer js = new JavaScriptSerializer ();      String Stujson = js. Serialize (Stu);      Console.WriteLine ("Using JavaScriptSerializer class serialization to get JSON-formatted data:");      Console.WriteLine (Stujson); example: JSON single-object serialization
string Dejson = Stujson;      dictionary<string, string> destu = js. Deserialize<dictionary<string, string>> (Dejson);      Console.WriteLine ("Using the JavaScriptSerializer class to deserialize JSON-formatted data into the specified class:");      foreach (string s in Stu. Keys)      {        Console.WriteLine (s.tostring () + "=" + Stu[s]. ToString ());      }      Console.ReadLine (); Example: JSON single object deserialization

Operation Result:

 2. Serialize and deserialize Json data using a third-party supported Json.NET (Newtonsoft.json) class library:

Vi. JavaScript and JSON

 1. Customize the JSON string in JavaScript and parse it;

Example: Student Zhao information:

function Myjson () {      var stu = {"id": "1", "name": "Zhao Da", "Age": "Three", "gender": "Male", "school": {"Class": "", "teach ER ":" Zhao Da's Teacher "}};      Alert ("id=" + stu.id + "," + "name=" + Stu.name + "," + "age=" + Stu.age + "," + "gender=" + Stu.gender);    }

After you can see the custom JSON string, the object can be automatically recognized as a member variable when using Stu.

2. JSON data passed in from the background is parsed and converted to JSON data objects:

Example: Student Zhao information (AJAX requests here are handled with jquery):

$ (document). Ready (function () {      $.ajax ({        URL: "ajaxweb.ashx",        Type: "GET",        dataType: "JSON",        Success:function (data) {          var stu = json.parse (data);          Alert ("id=" + stu.id + "," + "name=" + Stu.name + "," + "age=" + Stu.age + ",           " + "gender=" + Stu.gender + "," + "t Eacher= "+ stu.school[" Teacher "]);        },        error:function (e) {          alert (e.tostring ());})    }) Get a JSON string from the background and convert it to a JSON object


public void ProcessRequest (HttpContext context)    {      context. Response.Write (MyJson2 ());    }    private string MyJson2 ()    {      string json = "{\" id\ ": \" 1\ ", \" name\ ": \" Zhao da \ ", \" age\ ": \" 27\ ", \" Gender\ ": \" male \ ", \ "School\":       {\ "class\": \ "03\", \ "Teacher\": \ "Zhao Da's teacher \"} ";      JavaScriptSerializer js = new JavaScriptSerializer ();      JSON = JS. Serialize (JSON);      return JSON;    } Background Processing file code

Chrome under F12 developer Tools debugging:

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.