ASP tutorial. NET AJAX JSON Introduction tutorial and detailed instance JSON
Before we discuss the JSON format formally, let's briefly recall XML. XML is the abbreviation for "Extensible Markup Language," which provides a way to define a series of data transfer protocols in the Web, and is text-type, and is hailed as "an ideal way to fully develop the Internet and web potential".
So why do you want to introduce JSON in the ASP.net tutorial ajax? Let's take a look at the examples first. For example, the current Web page will load some address book information from the background, if written in XML, it may be the following form:
<contact>
<friend>
<name>michael</name>
<email>17bity@gmail.com</email>
</friend>
<friend>
<name>john</name>
<email>john@gmail.com</email>
</friend>
<friend>
<name>peggy</name>
<email>peggy@gmail.com</email>
</friend>
</contact>
And in JSON form, it would be:
[
Friend: {
Name: "Michael",
Email: "17bity@gmail.com",
Homepage: "Http://www.111cn.net"
},
Friend: {
Name: "John",
Email: "John@gmail.com",
Homepage: "Http://www.111cn.net"
},
Friend: {
Name: "Peggy",
Email: "Peggy@gmail.com",
Homepage: "Http://mb.111cn.net"
}
]
Use class
using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
User's summary description
</summary>
public class users
{
String name;
public string Name
{
get {return name;}
set {name = value;}
}
String age;
public string Age
{
get {return age;}
set {age = value;}
}
}
JS Code
<script type= "Text/jscript" >
function CallServer () {
JSON Send Object
Serversum ("{name: ' Linyijia ', Age: ' 21 '}");
}
function Getregister (RG, Contex) {
document.getElementById ("Txtregister"). Value=rg;
}
</script>
asp.net
using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.web.script.serialization;
Public partial class _default:system.web.ui.page, ICallbackEventHandler
{
Users u = NULL;
protected void Page_Load (object sender, EventArgs e)
{
Callback Getregister method
String callbackfun = Page.clientscript.getcallbackeventreference (This, "Arg", "Getregister", "context");
Creates the Serversum method, which, when invoked by the client, recalls the Getregister method, passes the argument to the RaiseCallbackEvent (string eventargument), and finally passes the
The GetCallbackResult () method passes the return value to the client
String registerfun = String.Format ("function serversum (arg,context) {{0};}}", Callbackfun);
Page.ClientScript.RegisterClientScriptBlock (This.gettype (), "Serversum", registerfun,true);
}
string mssage = String.Empty;
#region ICallbackEventHandler Members
public string GetCallbackResult ()
{
Return "server: Hello, your username is:" + u.name + "Your age is" + u.age;
}
public void RaiseCallbackEvent (String eventargument)
{
Web Effects Serializer js = new JavaScriptSerializer ();
U =js.deserialize<users> (eventargument);
}
#endregion
}