JSON. Net learning. Set serialization.

Source: Internet
Author: User
Serialization can be performed as long as the set implements the ienumable interface.

The JSON serializer provides excellent support for serialization and deserialization of collection objects.

-> Serializing

To serialize a set --- a generic list, array, Dictionary, or custom set --- simply call the serializer and use the set object you want to serialize as the parameter, JSON. net will serialize the set and all the values it contains.

Example:

public class Product        {            public string Name { get;    set; }            public decimal Price { get;    set; }            [JsonConverter(typeof(IsoDateTimeConverter))]            public DateTime ExpiryDate {    get; set; }        }

Test:

 

  Product p1 = new Product()                {                    Name = "Product    1",                    Price = 99.95m,                    ExpiryDate = new    DateTime(2010, 12, 19, 0, 0, 0, DateTimeKind.Utc)                };                Product p2 = new Product                {                    Name = "Product    2",                    Price = 12.50m,                    ExpiryDate = new    DateTime(2011, 1, 1, 0, 0, 0, DateTimeKind.Utc)                };                List<Product>    products = new List<Product>();                products.Add(p1);                products.Add(p2);                    string json =    JsonConvert.SerializeObject(products, Formatting.Indented);                Console.WriteLine(json);

Output result:

[
{
"Name": "Product 1 ",
"Price": 99.95,
"Expirydate": "2010-12-19t00: 00: 00Z"
},
{
"Name": "product 2 ",
"Price": 12.50,
"Expirydate": "2011-01-01t00: 00: 00Z"
}
]

-> Deserializing

To deserialize JSON to A. Net set, you only need to specify a set type that you want to deserialize. JSON. Net supports multiple types of sets.

Example:

 

 string json2 = @"[                                  {                                       ""Name"": ""Product 1"",                                       ""ExpiryDate"":    ""2010-12-19T00:00:00Z"",                                       ""Price"": 99.95,                                       ""Sizes"": null                                  },                                  {                                       ""Name"": ""Product 2"",                                       ""ExpiryDate"":    ""2011-01-01T00:00:00Z"",                                    ""Price"":    12.50,                                       ""Sizes"": null                                  }                                ]";                List<Product>    productList =    JsonConvert.DeserializeObject<List<Product>>(json2);                Console.WriteLine(productList.Count);//2                Product product1 =    productList[0];                Console.WriteLine(product1.Name);//Product 1
 

-> Deserializing dictionaries

Using JSON. net, you can also deserialize a json object to a. Net generic dictionary. The property name and attribute value of the JSON object will be added to the dictionary.

Example:

 

  string jsonDictionary =    @"{""key1"":""value1"",""key2"":""value2""}";  Dictionary<string,    string> dic = JsonConvert.DeserializeObject<Dictionary<string,string>>(jsonDictionary);  Console.WriteLine(dic.Count);//2  Console.WriteLine(dic["key1"]);//value1
 

 

 

 

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.