Converting, reading, and applying JSON data in Asp.net

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write, and easy to parse and generate by machines. It is based on JavaScript. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, and so on ). These features make JSON an ideal data exchange language.
 

JSON data has a strict format, which must be followed before it can be parsed. There are two main structures:

 

① Set of "name/value" PairsIt is understood as an object, structure, and associated array in different languages.
② Ordered list of valuesIn most languages, it is understood as an array. JSON indicates name/Value Pair: {"firstname": "Brett "}
 Multiple name/value pairs are listed together: {"firstname": "Brett", "lastname": "McLaughlin "}
In terms of syntax, this is not a great advantage over name/value pairs, but JSON is easier to use and more readable in this case. To represent a group of values, JSON not only improves readability, but also reduces complexity: {"employees ":[
 {"Firstname": "Brett", "lastname": "McLaughlin"},
 {"Firstname": "Jason", "lastname": "Hunter"},
 {"Firstname": "elliotte", "lastname": "Harold"}
]} 

What is the JSON data process used by Asp.net?

1 aspx to CS

2. Process aspx values in CS. After some processing, the values are processed in JSON format.

3. Return Aspx. The processed data that can be obtained in Aspx. Obtain the value in the form of a key-value pair.

 

Example 1:

Public class jsonhandler: ihttphandler
   {

              Public void processrequest (httpcontext context)
              {
                      Context. response. contenttype = "text/plain ";
                      String data = "[{Name: \" Tom \ ", age: \" 26 \ "},{ name: \" Jim \ ", age: \ "27 \"}] ";
                      Context. response. Write (data );
              }              Public bool isreusable
              {
                      Get
                      {
                              Return false;
                      }
              }
      } ② Parse JSON data at the front end <Head runat = "server">
      <Title> </title>      <SCRIPT src = "JS/jquery-1.3.2.js" type = "text/JavaScript"> </SCRIPT>      <SCRIPT type = "text/JavaScript">
              $ (Function (){
                      $. Getjson(
              "Jsonhandler. ashx ",
                Function (data ){
                        $. Each (data, function (I ){
                                $ ("# Cat-List"). append ("<li> name:" + Data [I]. Name
+ "Age:" + Data [I]. Age+ "</LI> ")
                        });
                });
              });  
      </SCRIPT></Head>
<Body>
      <Form ID = "form1" runat = "server">
      <Div>
      <Ul id = "cat-list"> </ul>
      </Div>
      </Form>
</Body>

Example 2:

Aspx

$. Getjson ("ajax/test. aspx", {ID: 1}, function (data ){

$. Each (data, function (I ){

Data [I]...

})

})

 

Test. CS

Public partial class getpoints: system. Web. UI. Page

{

List <student> List = Getlist (ID );

Javascriptserializer JSS = new javascriptserializer ();
Response. Write (JSS. serialize (list ));

Response. End ();
}

 

We can see the serialize method of the javascriptserializer object. What is the purpose of this method?

Model. Student itself has a name and other fields. The serialize method processes the data object into JSON format data.

[{"Num": "111", "name": "XY", "location": "China"}] ....... Pass Data [I]. NameTo obtain the value. Of course we can use the $. Ajax method to obtain the processed JSON data. $. Ajax ({
        Type: "Get ",
        Datatype: "JSON ",
        Data: {ID: JSON [I]. ID },
        Async: false,        URL: "ajax/test. aspx"Success: function (data ){
                                                                                 
          }
});

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.