Use newtonsoft. JSON. DLL to serialize and serialize objects 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'}]
. Reprinted from; http://www.cnblogs.com/feishan/archive/2008/06/18/1224463.html