A brief introduction to Json.NET
First introduce one for convenience in. NET in Api,json.net using Json. It makes it easy for us to read JSON objects that flow from the browser to the server and to write JSON objects in the response stream.
Json.NET only provides a server-side approach, primarily with classes that implement JSON text to convert to and from XML, Jsonreader classes and Jsonwriter classes with custom read and write JSON, and a JavaScriptSerializer class that does not customize read and write JSON.
asp.net Ajax, the server driven by several methods for JavaScriptSerializer classes to implement serialization and deserialization capabilities. In Json.NET, the server-side serialization and deserialization capabilities are mainly provided by several methods of the Javascriptconvert class. The examples in this article use only the Javascriptconvert.
Javascriptconvert
In Json.NET, this class is used to serialize and deserialize JavaScript objects.
This class has two methods:
- SerializeObject (object value, params jsonconverter[] converters), serialized, it has an overloaded method SerializeObject (object value)
- Deserializeobject (string value, type type) deserialized, it has an overloaded method Deserializeobject (string value)
On the client side, Json.NET does not provide support.
Here we try to use this API to implement JSON interaction data in asp.net.
A simple example of using json.net to interact with JSON data in C/s
1. Create a new ASP.net website first.
2, will download to the binary folder Newtonsoft.Json.dll and Newtonsoft.Json.XML into the site's bin file, of course, first new bin folder. Then add a reference to the DLL.
3, switch to the design mode, from the standard toolbox to add three Label,text to the page, respectively, EmployeeID, EmployeeName, EmployeeInfo; three Textbox,id txtID, Txtname, Txtinfo, respectively. ; then add a button,id to Btntojsonstring,text for invoke toJSONString, and then add a textbox,id for Txtjson,textmode for Multiline,rows set to 5 And then add a button and Textbox,id as Btntoobject, Txtstremployee,button text for Invoke Tostremployee, respectively.
4, add a WebService project.
Write an employee class, then two WebMethod, and then add a reference to the Web service in your project. The code is as 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>///WebService Summary description///</summary> [WebService (Namespace = "http://tempuri.org/")] [We Bservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)] public class WebService:System.Web.Services.WebService {p
Ublic WebService () {//If you are using a design component, uncomment the following line//initializecomponent (); [WebMethod] public string tojsonstring (int EmployeeID, String employeename, string[] employeeinfo) {Employe
E employee = new Employee (); Employee.
EmployeeID = EmployeeID; Employee.
EmployeeName = EmployeeName; Employee.
EmployeeInfo = EmployeeInfo; Return Javascriptconvert.seriAlizeobject (employee); [WebMethod] public string Tostremployee (string strjson) {Employee Decerializedemployee = (employee) Javascrip
Tconvert.deserializeobject (Strjson, typeof (Employee)); Return "ID:" + Decerializedemployee.employeeid + "" + "Name:" + decerializedemployee.employeename + "" + "
Info: "+ decerializedEmployee.EmployeeInfo.ToString (); }
}
The member's property types are numeric, string, and array respectively.
5. Write event code for two button
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 operation, in EmployeeID, EmployeeName, EmployeeInfo input 123, HUNTS.C and some personal information (separated by commas), click Invoke toJSONString, after the server-side serialization, The result is in the Txtjson text box, and then click Invoke Tostremployee, at which point the JSON text in the Txtjson text box is transferred to the server side and the server side reads the JSON and deserializes it into an object. The member value of employee is then written in Txtstremployee.
How to use JSON in asp.net is introduced here, I hope this article is helpful to everyone's study.