Implemented through javascriptserializer. Its namespace is: system. Web. Script. serialization
To use it, you must also add
System. Web. Extensions library file reference
Reference object class: customer
Public Class Customer
{
Public int unid {Get; set ;}
Public String customername {Get; set ;}
}
Description: used to provide serialization and deserialization functions for afax-enabled applications.
(1) serialization
Method: Public String serialize (Object OBJ), used to convert an object to a JSON string
Public String scriptserialize (customer)
{
Javascriptserializer JS = new javascriptserializer ();
Return Js. serialize (customer );
}
Test
Customer cc = new customer {unid = 1, customername = "John "};
String strjson = scriptserialize (CC );
Console. writeline (strjson );
(2) deserialization
Public customer scriptdeserialize (string strjson)
{
Javascriptserializer JS = new javascriptserializer ();
Return Js. deserialize <customer> (strjson );
}
Implemented using the deserialize <t> method.
Test:
Customer C1 = scriptdeserialize (strjson );
Console. writeline (c1.unid + "" + c1.customername );
(3) method generics
Public String scriptserialize <t> (T)
{
Javascriptserializer JS = new javascriptserializer ();
Return Js. serialize (t );
}
Public t scriptdeserialize <t> (string strjson)
{
Javascriptserializer JS = new javascriptserializer ();
Return Js. deserialize <t> (strjson );
}
Test:
Customer cc = new customer {unid = 1, customername = "John "};
String strjson = scriptserialize <customer> (CC );
Console. writeline (strjson );
Customer C1 = scriptdeserialize <customer> (strjson );
Console. writeline (c1.unid + "" + c1.customername );