Serialize objects using JSON in Asp.net 3.5 (two methods)

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.