TechieClues TechieClues
Updated date Jan 10, 2023
In this blog, we will learn how to convert an object to a JSON string in C#.
  • 5.8k
  • 0
  • 0

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

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

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

Comments (0)

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