ASP. net3.5 has integrated the method of serializing the object as JSON.
1: system. runtime. serialization. JSON;
2: different methods in the two namespaces system. Web. Script. serialization are serialized and deserialized.
Method 1: system. runtime. serialization. JSON
Public Class Jsonhelper
{
/// <Summary>
/// Generate JSON format
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "OBJ"> </param>
/// <Returns> </returns>
Public Static String Getjson < T > (T obj)
{
Datacontractjsonserializer JSON = New Datacontractjsonserializer (obj. GetType ());
Using (Memorystream stream = New Memorystream ())
{
JSON. writeobject (stream, OBJ );
String Szjson = Encoding. utf8.getstring (stream. toarray ()); Return Szjson;
}
}
/// <Summary>
/// Obtain the JSON model.
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "szjson"> </param>
/// <Returns> </returns>
Public Static T parsefromjson < T > ( String Szjson)
{
T OBJ = Activator. createinstance < T > ();
Using Memorystream MS = New Memorystream (encoding. utf8.getbytes (szjson )))
{
Datacontractjsonserializer serializer = New Datacontractjsonserializer (obj. GetType ());
Return (T) serializer. readobject (MS );
}
}
}
Public Class Topmenu
{
Public String Id { Get ; Set ;}
Public String Title { Get ; Set ;}
Public String Defaulturl { Get ; Set ;}
}
Topmenu t_menu = New Topmenu ()
{
ID = " 1 " ,
Title = " Global " ,
Defaurl URL = " 123456 "
};
List < Topmenu > Rochelle topmenu = New List < Topmenu > ();
For ( Int I = 0 ; I < 3 ; I ++ )
{
Rochelle topmenu.add (t_menu );
}
Response. Write (jsonhelper. getjson < List < Topmenu > (L_topmenu ));
Output result:
[{"Defaulturl": "123456", "ID": "1", "title": "Global" },{ "defaulturl": "123456", "ID ": "1", "title": "Global" },{" defaulturl ":" 123456 "," ID ":" 1 "," title ":" Global "}]
The following uses the preceding parsefromjson method to read JSON
Output result: Global
String Szjson = @" {"" ID "": "" 1 "", "" title ":" "Global" "," "defaulturl" ":" 123456 ""} " ;
Topmenu t_menu2 = Jsonhelper. parsefromjson < Topmenu > (Szjson );
Response. Write (t_menu2.title );
Method 2: system. Web. Script. serialization(Reference System. Web. Extensions. dll) still uses the JSON Attribute Class in the above method. The serialization method is different.
Topmenu t_menu = New Topmenu ()
{
ID = " 1 " ,
Title = " Global " ,
Defaurl URL = " 123456 "
};
List<Topmenu>Rochelle topmenu= NewList<Topmenu>();
For(IntI= 0; I< 3; I++)
{
Rochelle topmenu.add (t_menu );
}
The output is as follows:
Javascriptserializer JSS = New Javascriptserializer ();
Response. Write (JSS. serialize (l_topmenu ));
The output result is the same.
[{"Defaulturl": "123456", "ID": "1", "title": "Global" },{ "defaulturl": "123456", "ID ": "1", "title": "Global" },{" defaulturl ":" 123456 "," ID ":" 1 "," title ":" Global "}]
The deserialize method in javascriptserializer reads JSON
String Szjson = @" {"" ID "": "" 1 "", "" title ":" "Global" "," "defaulturl" ":" 123456 ""} " ;
Topmenu toptabmenu = JSS. deserialize < Topmenu > (Szjson );
Response. Write (JSS. serialize (toptabmenu. Title ));
Output result: Global
To sum up:The two methods are advantageous. Flexible. [Scriptignore], which allows JSON serialization to ignore it and thus not output it. Not serialized.