Before discussing the json format, let's briefly recall the XML. XML is short for "Extensible Markup Language". It provides a method to define a series of data transmission protocols on the Web, which is text-based, it is hailed as "an ideal way to fully develop the potential of the Internet and Web ".
Asp tutorial. net ajax json getting started tutorial and detailed instance json Introduction
Before formally discussing the json format, let's briefly recall the xml. Xml is short for "Extensible Markup Language". It provides a method to define a series of data transmission protocols on the web, which is text-based, it is hailed as "an ideal way to fully develop the potential of the internet and web ".
So why should json be introduced in asp.net ajax? Let's take a look at the example. For example, the current web page will load some address book information from the background. If the information is written in xml, it may be in the following format:
<Contact>
<Friend>
<Name> michael </name>
<Email> 17bity@gmail.com </email>
<Homepage> http://www.bKjia. c0m </Friend>
<Friend>
<Name> john </name>
<Email> john@gmail.com </email>
<Homepage> http://www.zhutiai.com </Friend>
<Friend>
<Name> peggy </name>
<Email> peggy@gmail.com </email>
<Homepage> http://www.aimeige.com.cn </Friend>
</Contact>
The json format is:
[
Friend :{
Name: "michael ",
Email: 17bity@gmail.com ",
Homepage: "http://www.bKjia. c0m"
},
Friend :{
Name: "john ",
Email: john@gmail.com ",
Homepage: "http://www.bKjia. c0m"
},
Friend :{
Name: "peggy ",
Email: peggy@gmail.com ",
Homepage: "http://mb.bKjia. c0m"
}
]
Use class
Using system;
Using system. collections. generic;
Using system. linq;
Using system. web;
/// <Summary>
/// User Summary
/// </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 sending 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)
{
// Call back the getregister Method
String callbackfun = page. clientscript. getcallbackeventreference (this, "arg", "getregister", "context ");
// Create the serversum method. When the client calls the method, it calls back the getregister method, passes the parameter to raisecallbackevent (string eventargument), and finally passes
// The getcallbackresult () method sends the returned 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 Member
Public string getcallbackresult ()
{
Return "server: Hello, your username is:" + u. name + "your age is" + u. age;
}
Public void raisecallbackevent (string eventargument)
{
Webpage special effects serializer js = new javascriptserializer ();
U = js. deserialize <users> (eventargument );
}
# Endregion
}