Use newtonsoft. JSON. DLL to serialize and serialize objects

Source: Internet
Author: User
Download from here: http://www.newtonsoft.com/products/json/
Installation:
1 . Decompress the downloaded file to obtain newtonsoft. JSON. dll.
2. Add reference in the project ..
Serialization and deserialization in the. Net project:
 
ProductProduct =New Product();
 
 
 
Product. Name ="Apple";
 
Product. Expiry =New Datetime(2008, 12, 28 );
 
Product. Price = 3.99 m;
Product. Sizes =New String[] {"Small","Medium","Large"};
 
 
 
StringOutput =Javascriptconvert. Serializeobject (product );
 
//{
 
// "Name": "apple ",
 
// & Quot; expiry & quot;: new date (1230422400000 ),
 
// "Price": 3.99,
 
// "Sizes ":[
// "Small ",
 
// "Medium ",
 
// "Large"
 
//]
 
//}
 
 
 
ProductDeserializedproduct = (Product)Javascriptconvert. Deserializeobject (output,Typeof(Product));


Read JSON

StringJsontext ="['Json! ', 1, true, {property: 'value'}]";
 
 
 
JsonreaderReader =New Jsonreader(New Stringreader(Jsontext ));
 
 
 
Console. Writeline ("Tokentype \ t \ tvaluetype \ t \ tvalue");
 
 
 
While(Reader. Read ())
 
{
Console. Writeline (reader. tokentype +"\ T"+ Writevalue (reader. valuetype) +"\ T"+ Writevalue (reader. Value ))
 
}

Result:

Tokentype Valuetype Value
Startarray Null Null
String System. String JSON!
Integer System. int32 1
Boolean System. Boolean True
Startobject Null Null
Propertyname System. String Property
String System. String Value
Endobject Null Null
Endarray Null Null
JSON writing
StringwriterSw =New Stringwriter();
 
JsonwriterWriter =New Jsonwriter(SW );
 
 
 
Writer. writestartarray ();
 
Writer. writevalue ("JSON! ");
 
Writer. writevalue (1 );
 
Writer. writevalue (True);
 
Writer. writestartobject ();
 
Writer. writepropertyname ("Property");
Writer. writevalue ("Value");
 
Writer. writeendobject ();
 
Writer. writeendarray ();
 
 
 
Writer. Flush ();
 
 
 
StringJsontext = Sw. getstringbuilder (). tostring ();
 
 
 
Console. Writeline (jsontext );
 
// ['Json! ', 1, true, {property: 'value'}]

The output is as follows:['Json! ', 1, true, {property: 'value'}].

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.