Asp.net uses JSON

Source: Internet
Author: User

This article will briefly introduce an API that implements JSON in. net, and then use this API to do a small exercise in C/s asp. NET.

Brief Introduction to JSON. net

First, we will introduce a JSON. Net API to facilitate the use of JSON in. net. It allows us to easily read the JSON object from the browser to the server and write the JSON object in the response stream. Download JSON. net.

JSON. net only provides server-side methods, mainly including classes for converting JSON text and XML, jsonreader class for custom reading and writing JSON, and jsonwriter class, there is also a non-custom javascriptserializer class for reading and writing JSON.

In ASP. NET Ajax, the server uses several methods of the javascriptserializer class to implement serialization and deserialization. In JSON. net, the serialization and deserialization capabilities of the server are mainly provided by several methods of the javascriptconvert class. In this example, only javascriptconvert is used.

    1. Javascriptconvert

      • In JSON. net, this class is used to serialize and deserialize JavaScript objects.
      • This class has two methods:
        1. Serializeobject (object value, Params jsonconverter [] converters), serialization, it has an overloaded method serializeobject (object value)
        2. Deserializeobject (string value, type), deserialization, it has a heavy load method deserializeobject (string value)

JSON. NET is not supported on the client. If necessary, you can use the JSON. js mentioned in the previous article "What Is JSON: First-recognized JSON" to process serialization and deserialization of the client.

Next we try to use this API to implement JSON Interactive Data in ASP. NET.

A simple example of using JSON. Net to interact JSON data in C/S
  1. Create an ASP. NET Website first.

  2. Add newtonsoft. JSON. dll and newtonsoft. JSON. XML in the downloaded binary folder to the binfile of the website. Of course, you must create a new bin folder. Then add a reference to the DLL.

  3. Switch to the design mode, and add three labels to the page from the standard toolbox. The texts are respectively employeeid, employeename, and employeeinfo. The three textbox IDs are txtid, txtname, and txtinfo; then add a button with the ID btntojsonstring and the text as invoke tojsonstring. Then add a textbox with the ID txtjson, textmode as multiline, and rows as 5; then add a button and textbox respectively. The ID is btntoobject and txtstremployee. The text of the button is invoke tostremployee.
  4. Add a WebService project.
    • Write an employee class, then two webmethods, and add references to the Web service in the project.CodeAs follows:

      Using System;
      Using System. Web;
      Using System. collections;
      Using System. Web. Services;
      Using System. Web. Services. Protocols;
      Using Newtonsoft. JSON;

      Class Employee
      {
      Private   String [] Employeeinfo;

      Public   Int Employeeid;
      Public   String Employeename;
      Public   String [] Employeeinfo
      {
      Get { Return   This . Employeeinfo ;}
      Set { This . Employeeinfo = Value ;}
      }
      }

      /**/ ///   <Summary>
      /// Summary of WebService
      ///   </Summary>
      [WebService (namespace =   " Http://tempuri.org/ " )]
      [Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
      Public   Class WebService: system. Web. Services. WebService {

      PublicWebService (){

      //If you use the designed components, uncomment the following lines:
      //Initializecomponent ();
      }

      [Webmethod]
      Public   String Tojsonstring ( Int Employeeid, String Employeename, String [] Employeeinfo)
      {
      Employee =   New Employee ();
      Employee. employeeid = Employeeid;
      Employee. employeename = Employeename;
      Employee. employeeinfo = Employeeinfo;

      ReturnJavascriptconvert. serializeobject (employee );
      }

      [Webmethod]
      Public   String Tostremployee ( String Strjson)
      {
      Employee decerializedemployee = (Employee) javascriptconvert. deserializeobject (strjson, Typeof (Employee ));
      Return   " ID: "   + Decerializedemployee. employeeid +   " "
      +   " Name: "   + Decerializedemployee. employeename +   " "
      +   " Info: "   + Decerializedemployee. employeeinfo. getvalue (0). tostring ()
      }
      }

The attribute types of members are numbers, strings, and arrays.

5. Write Event code for two buttons

 

Protected   Void Btntojsonstring_click ( Object Sender, eventargs E)
{
Myserv. WebService mywebserv =   New Myserv. WebService ();
String Employeejson = Mywebserv. tojsonstring (int32.parse (txtid. Text), txtname. Text, txtinfo. Text. Split ( ' , ' ));
Txtjson. Text = Employeejson;
}
Protected   Void Btntostremployee_click ( Object Sender, eventargs E)
{
Myserv. WebService mywevserv =   New Myserv. WebService ();
String Stremployee = Mywevserv. tostremployee (txtjson. Text );
Txtstremployee. Text = Stremployee;
}

 

 

6. Press Ctrl + F5 and enter 123 and hunts in employeeid, employeename, and employeeinfo. C and some personal information (separated by commas); click invoke tojsonstring, and the result is serialized on the server side in the txtjson text box; then click invoke tostremployee, in this case, the JSON text in the txtjson text box is transmitted to the server. The server reads the JSON text and deserializes it into an object. Then, the employee's member value is written into txtstremployee.

\

You only need to know that name (name) is value (value) in JSON, and the value has several formats. If it is a number, no quotation marks are required. If it is a string, if [] is used as an array, if {} appears, it indicates that it is nested JSON. And so on.

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.