Introduction : 

In this C# tutorial we will see how to serialize & desirialize json in c# .Net.

Serialization & Deserialization in C# .Net :

These days, a common format for transferring data across programmes is JSON data. While creating a.NET application, converting JSON data to.NET objects and vice versa is fairly typical. Deserialization is the process of converting JSON data back into.NET objects, while serialisation is the process of converting.NET objects, such as strings, into JSON data. In this essay and the accompanying code samples, we will first study how to serialise JSON in C# before learning how to deserialize JSON in C#.

What is Json ?

JSON, or JavaScript Object Notation, is a simple format for exchanging data. JSON is a text format that is totally agnostic of language. Humans can read and write it with ease, and machines can analyse and generate it with ease as well.

The following two data structures are supported by JSON:

  1. Different programming languages support this data structure, which is a collection of name/value pairs.
  2. Values in an ordered list; can be an array, list, vector, sequence, etc.

Using JSon .Net Library in C# .Net :

Json.NET is a third-party package that facilitates the JsonSerializer’s translation of JSON text to.NET objects. By mapping the property names of.NET object objects to the property names of JSON objects, the JsonSerializer converts.NET objects into their JSON equivalent text and back again. It is free to use for commercial applications and open-source software.
The following are the some features :

1. LINQ to JSON allows for manual reading and writing of JSON, while a flexible JSON serializer allows for conversion between.NET objects and JSON.

2. Superior to the built-in JSON serializers in.NET in terms of performance.

3. Simple JSON to read.

4. Transform JSON into and out of XML.

5. supports Silverlight, Windows Phone,.NET 2,.NET 3.5, and.NET 4.

In Visual Studio, go to Tools Menu -> Choose Library Package Manager -> Package Manager Console. It opens a command window where we need to put the following command to install Newtonsoft.Json.

Install-Package Newtonsoft.Json .

 

Serialization :

During serialisation, a customized.Net object is transformed into a JSON string. It creates a BlogPostsclass instance and gives some values to its properties in the code that follows. Following that, the static method SerializeObject() of the JsonConvert class is invoked with the object (BlogPosts) as a parameter. JSON data is returned as strings.

Example :

Deserialization :

Deserialization turns a JSON string into a specific.Net object, the opposite of what Serialisation does. The JsonConvert class’ static DeserializeObject() method is invoked in the following code by supplying JSON data. It takes JSON data and outputs a unique object (BlogPosts).

Example :

For any Help or Queries Contact us on info@crmonce.com or +918096556344