TechieClues . TechieClues .
Updated date Aug 21, 2022
In this blog, we will learn how to convert an object to a JSON string in C#.

This blog explains how to convert an object to a JSON string using C#.

 

We will use the below methods to convert an object to a JSON string,

  • Convert an Object to a JSON String Using JsonConvert.SerializeObject().
  • Convert an Object to a JSON String Using JObject.FromObject().
  • Convert a List to a JSON String.

Convert an Object to a JSON String Using JsonConvert.SerializeObject():

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

 The JsonConvert.SerializeObject() method is used to convert an object to the minified JSON string.

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)
        {
            Employee employee = new Employee
            {
                Id = 100,
                Name = "Sabari",
                Age = 30
            };
            // JsonConvert.SerializeObject method converts employee object into JSON string
            var jsonObject = JsonConvert.SerializeObject(employee);
            Console.WriteLine(jsonObject);
            Console.ReadKey();
        }
    }
}

Output:

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

Convert an Object to a JSON String Using JObject.FromObject():

We will also use the JObject.FromObject() method to convert an object to the formatted JSON string.

using Newtonsoft.Json.Linq;
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)
        {
            Employee employee = new Employee
            {
                Id = 100,
                Name = "Sabari",
                Age = 30
            };
            // JObject.FromObject() method converts employee object into JSON string
            var jsonObject = JObject.FromObject(employee);
            Console.WriteLine(jsonObject);
            Console.ReadKey();
        }
    }
}

Output:

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

Convert a List to a JSON String:

We can also convert the list object to a JSON string using JsonConvert.SerializeObject() method 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)
        {
            Employee[] employee = new Employee[]
            {
                new Employee()
                {
                    Id = 100,
                    Name = "Sabari",
                    Age = 30
                },
                new Employee()
                {
                    Id = 101,
                    Name = "John",
                    Age = 28
                },
                new Employee()
                {
                    Id = 102,
                    Name = "Peter",
                    Age = 35
                }

            };
            // JsonConvert.SerializeObject method converts employee list into a JSON string
            var jsonObject = JsonConvert.SerializeObject(employee);
            Console.WriteLine(jsonObject);
            Console.ReadKey();
        }
    }
}

Output:

[{"Id":100,"Name":"Sabari","Age":30},{"Id":101,"Name":"John","Age":28},{"Id":102,"Name":"Peter","Age":35}]

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

ABOUT THE AUTHOR

TechieClues .
TechieClues .
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

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