How to convert JSON to Object in C# Examples|Serialize and deserialize
JSON
is abbreviated as javascript object notation, which stores text-based keys and values.
Let us first understand what is JSON type. It is a text-based file format that is used to save and transfer data between software applications. Usually, the browser sends the request data to the server and receives the response data in JSON format.
Letβs declare a JSON object like this.
{
"name": "Tom",
"id": "1",
"age": 25
}
Letβs define the C# class for holding the json data,
Create properties and fields as per the JSON structure of a json object.
public class Employee
{
public string name { get; set; }
public string id { get; set; }
public integer age { get; set; }
}
Convert JSON to C# Object class
In multiple ways, we can convert a JSOn string to C# Class objects.
use Newtonsoft.Json package
First, Install Newtonsoft packages with the latest version using the NuGet package manager in your project
Install-Package Newtonsoft.Json
or use the below code
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>
run `dotnet restore command
Import
Newtonsoft.Json module into your codeusing Newtonsoft.Json;
Create a class with the same properties as per the JSON structure
DeserializeObject
inJsonConvert
takes JSOn object as input and converts into ClassYou can access properties using class name.property
Here is an example
using System;
using Newtonsoft.Json;
public class Employee {
public string name {
get;
set;
}
public string id {
get;
set;
}
public int age {
get;
set;
}
}
public class Program {
public static void Main() {
string jsonString = @"{""name"": ""Tom"",""id"": ""1"",""age"": 25}";
Console.WriteLine(jsonString);
// π Convert JSON String to an Object
var employee = Newtonsoft.Json.JsonConvert.DeserializeObject < dynamic > (jsonString);
Console.WriteLine(" π Printing an Object properties π");
Console.WriteLine(employee.name);
Console.WriteLine(employee.age);
Console.WriteLine(employee.id);
}
}
Output:
{"name": "Tom","id": "1","age": 25}
π SortedDictionary keys ascending Defaul π
Tom
25
1
How to convert Class Object to JSON in C#
Newtonsoft
provides Newtonsoft.Json.JsonConvert.SerializeObject object that takes an object and returns a json object. It returns a simple json object without format.
if Newtonsoft.Json.Formatting.Indented
property is passed, It returns a JSON string in indented prettyprint style
using System;
using Newtonsoft.Json;
public class Employee {
public string name {
get;
set;
}
public string id {
get;
set;
}
public int age {
get;
set;
}
}
public class Program {
public static void Main() {
string jsonString = @"{""name"": ""Tom"",""id"": ""1"",""age"": 25}";
// π Convert JSON String to an Object
var employee = Newtonsoft.Json.JsonConvert.DeserializeObject < dynamic > (jsonString);
// π Convert Object to JSON String to an Object
string json = Newtonsoft.Json.JsonConvert.SerializeObject(employee);
string jsonFormatted = Newtonsoft.Json.JsonConvert.SerializeObject(employee, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(" π Printing an JSON object with or without formatting π");
Console.WriteLine(json);
Console.WriteLine(jsonFormatted);
}
}
Output:
π Printing an JSON object with or without formatting π
{"name":"Tom","id":"1","age":25}
{
"name": "Tom",
"id": "1",
"age": 25
}