Jquery has recently been studied. $. Ajax is used to exchange data with the background in JSON format. It has been a long time before the background and foreground serialization and deserialization methods have been studied.
JS files called at the front end need to be usedJQuery-1.2.6.jsAndJson2.js, Background reference spaceSystem. runtime. serializationAndSystem. runtime. serialization. JSON
JSON serialization and deserialization Methods
/// <Summary>
/// JSON serialization, used to send to the client
/// </Summary>
Public Static String Tojsjson ( This Object Item)
{
Datacontractjsonserializer serializer= NewDatacontractjsonserializer (item. GetType ());
UsingMemorystream MS= NewMemorystream ())
{
Serializer. writeobject (MS, item );
Stringbuilder sb= NewStringbuilder ();
SB. append (encoding. utf8.getstring (Ms. toarray ()));
ReturnSB. tostring ();
}
}
/// <Summary>
/// JSON deserialization, used to generate the corresponding object after receiving the JSON from the client
/// </Summary>
Public Static T fromjsonto < T > ( This String Jsonstring)
{
Datacontractjsonserializer Ser= NewDatacontractjsonserializer (Typeof(T ));
Memorystream MS= NewMemorystream (encoding. utf8.getbytes (jsonstring ));
T jsonobject=(T) SER. readobject (MS );
Ms. Close ();
ReturnJsonobject;
}
Entity class
[Datacontract]
Public Class Testobj
{
[Datamember]
Public String Make { Get ; Set ;}
[Datamember]
Public String Model { Get ; Set ;}
[Datamember]
Public Int Year { Get ; Set ;}
[Datamember]
Public String Color { Get ; Set ;}
}
-------------------------------------------- JavaScript to obtain JSON ---------------------------------
Javascript call TestCode
$ ( ' # Getjson ' ). Click ( Function (){
$. Ajax ({
URL: " Getjsonhandler. ashx " ,
Type: ' Get ' ,
Data :{},
Datatype: ' JSON ' ,
Timeout: 1000 ,
Error: Function (XMLHttpRequest, textstatus, errorthrown) {alert (textstatus )},
Success: Function (Result ){
Alert (result. Make );
Alert (result. model );
Alert (result. year );
Alert (result. Color );
}
});
});
C # background code generation
Public Class Getjsonhandler: ihttphandler
{
Public Void Processrequest (httpcontext context)
{
Testobj = New Testobj ();
OBJ. Make = " Make is value " ;
OBJ. Model = " Model is value " ;
OBJ. Year = 999 ;
OBJ. Color = " Color is value " ;
Context. response. Write (obj. tojsjson ());
}
Public Bool Isreusable
{
Get
{
Return False ;
}
}
}
//Return Value: {"color": "color is value", "make": "Make is value", "model": "model is value", "year": 999}
------------------------------- C # object generated by JSON ---------------------------------------
Javascript call test code
$ ( ' # Postjson ' ). Click ( Function (){
VaR M_obj = {Make: " Dodge " , Model: " Coronet R/T " , Year: 1968 , Color: " Yellow " };
VaR Jsonstr = JSON. stringify (m_obj ); // Use json2.js to generate a JSON string
$. Ajax ({
URL: " Postjsonhandler. ashx " ,
Type: ' Post ' ,
Data: {postjson: jsonstr },
Datatype: ' JSON ' ,
Timeout: 1000 ,
Error: Function (XMLHttpRequest, textstatus, errorthrown) {alert (textstatus )},
Success: Function (Result ){
Alert (result. Success );
}
});
});
C # background code generation
Public Class Postjsonhandler: ihttphandler
{
Public Void Processrequest (httpcontext context)
{
String Jsonstr = Context. request [ " Postjson " ];
Testobj=Jsonstr. fromjsonto<Testobj>();
If(String. Isnullorempty (obj. Make)| String. Isnullorempty (obj. Model)| String. Isnullorempty (obj. color)
| OBJ. Year < 0 )
{
Context. response. Write ( " {Success: false} " );
}
Else
{
Context. response. Write ( " {Success: true} " );
}
Public BoolIsreusable
{
Get
{
Return False;
}
}
}
note the following when using JSON, when the server pieces together to generate a JSON string, be sure to wrap the string with \ "\". Otherwise, the client will certainly report an error and generate an object based on the JSON string, is assigned based on the corresponding name, more than or less will not report an error.