Use newtonsoft. JSON to convert, read, and write data in. net.

Source: Internet
Author: User
Using newtonsoft. JSON;
Using newtonsoft. JSON. converters;

// Deserializes a json string into an object
Target object = javascriptconvert. deserializeobject (JSON string, typeof (target object ));
// Serialize the target object into a JSON string

String JSON string = javascriptconvert. serializeobject (target object );

1. Reference newtonsoft. JSON. dll
2. Add reference in the project ..
Serialization and deserialization in the. Net project:

Product product = new Product();
 
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
 
string output = JavaScriptConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": new Date(1230422400000),
//  "Price": 3.99,
//  "Sizes": [
//     "Small",
//     "Medium",
//     "Large"
//  ]
//}
 
Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));

Read JSON

string jsonText = "['JSON!',1,true,{property:'value'}]";
 
JsonReader reader = new JsonReader(new StringReader(jsonText));
 
Console.WriteLine("TokenType\t\tValueType\t\tValue");
 
while (reader.Read())
{
     Console.WriteLine(reader.TokenType + "\t\t" + WriteValue(reader.ValueType) + "\t\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
StringWriter sw = new StringWriter();
JsonWriter writer = 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();
 
string jsonText = sw.GetStringBuilder().ToString();
 
Console.WriteLine(jsonText);
// ['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.