Mohanapriya R Mohanapriya R
Updated date May 20, 2024
In this article, we will learn how to easily convert JSON objects to strings in C# with practical examples and output.
  • 1.2k
  • 0
  • 0

How to Convert JSON Objects to Strings in C#?

Consider the following C# code snippet for converting JSON objects to strings in C# :

using System;
using Newtonsoft.Json;

class Program
{
    static void Main()
    {
        // Creating a sample JSON object
        var jsonObject = new
        {
            Name = "Dan Micheal",
            Age = 35,
            Occupation = "Software Developer"
        };

        // Converting JSON object to string
        string jsonString = JsonConvert.SerializeObject(jsonObject);

        // Outputting the converted string
        Console.WriteLine(jsonString);
    }
}

In this example, we create a sample JSON object representing a person's details: name, age, and occupation. Then, we use the JsonConvert.SerializeObject() method from the popular Newtonsoft.Json library to convert this JSON object to a string. Finally, we print the resulting string to the console.

Output

{"Name":"Dan Micheal","Age":35,"Occupation":"Software Developer"}

Comments (0)

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