TechieClues TechieClues
Updated date Aug 21, 2022
We will learn how to Convert a JSON String to an Object in C# in this blog.
  • 1.3k
  • 0
  • 0

 

We can easily convert the JSON string to an object using the Newtonsoft.Json framework.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. Humans can easily read and write JSON. Also, easy for machines to parse and generate.

JSON String:

{"Id":100,"Name":"Sabari","Age":30}

We will convert the above JSON string into the below class object,

public class Employee
{
	public int Id { get; set; }
	public string Name { get; set; }
	public int Age { get; set; }
}

In this blog, we will see how to,

  • Convert a JSON string to an Object Using JsonConvert.DeserializeObject().
  • Convert a JSON Array String to a List Using JsonConvert.DeserializeObject().

Convert a JSON string to an Object Using JsonConvert.DeserializeObject():

In order to use the JsonConvert.DeserializeObject() method, we need to install the Newtonsoft.Json package using the NuGet package manager or console as shown below,

 The JsonConvert.DeserializeObject() method is used to convert a JSON string to an object.

var jsonString = "{\"Id\":100,\"Name\":\"Sabari\",\"Age\":30}";
// JsonConvert.DeserializeObject method converts JSON string into a object.
var employee = JsonConvert.DeserializeObject<Employee>(jsonString);

Full Code Snippet:

using Newtonsoft.Json;
using System;

namespace JSONConvertion
{
    class Program
    {
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
        static void Main(string[] args)
        {
            var jsonString = "{\"Id\":100,\"Name\":\"Sabari\",\"Age\":30}";
            // JsonConvert.DeserializeObject method converts JSON string into a object.
            var employee = JsonConvert.DeserializeObject<Employee>(jsonString);
            Console.WriteLine("Employee Id: " + employee.Id);
            Console.WriteLine("Employee Name: " + employee.Name);
            Console.WriteLine("Employee Age: " + employee.Age);
            Console.ReadKey();
        }
    }
}

Output:

Employee Id: 100
Employee Name: Sabari
Employee Age: 30

Convert a JSON Array String to a List Using JsonConvert.DeserializeObject():

We can also convert the JSON array string into a list using JsonConvert.DeserializeObject() as shown below,

using Newtonsoft.Json;
using System;

namespace JSONConvertion
{
    class Program
    {
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
        static void Main(string[] args)
        {
            var jsonString = "[{\"Id\":100,\"Name\":\"Sabari\",\"Age\":30},{\"Id\":101,\"Name\":\"John\",\"Age\":28},{\"Id\":102,\"Name\":\"Peter\",\"Age\":35}]";
            // JsonConvert.DeserializeObject method converts JSON string into a object.
            var employees = JsonConvert.DeserializeObject<Employee[]>(jsonString);
            foreach(var employee in employees)
            {
                Console.WriteLine("Employee Id: " + employee.Id);
                Console.WriteLine("Employee Name: " + employee.Name);
                Console.WriteLine("Employee Age: " + employee.Age);
                Console.WriteLine("---------------------------");
            }
            Console.ReadKey();
        }
    }
}

Output:

Employee Id: 100
Employee Name: Sabari
Employee Age: 30
---------------------------
Employee Id: 101
Employee Name: John
Employee Age: 28
---------------------------
Employee Id: 102
Employee Name: Peter
Employee Age: 35
---------------------------

Please look at this blog for How to Convert an Object to a JSON String in C#.

ABOUT THE AUTHOR

TechieClues
TechieClues

We will post the articles, blogs or interview questions and answers in Python, PHP, Java, C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices, VB.NET, .Net Core, ...Read More

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!