You can convert an object to JSON using the built-in System.Text.Json or
Newtonsoft.Json(also known as JSON.NET) libraries. Both libraries provide methods to serialize C# objects into
JSON format.
Here's how you can do it using both libraries:
Using ‘System.Text.JSON’
using System.Text.JSON;
using System;
namespace ConsoleApplication1
{
public class ObjectToJSON
{
public static void Main(string[] args)
{
ObjectToJSON.Convert();
Console.ReadLine();
}
public static string Convert()
{
// Define an object
var obj = new
{
Name = "John",
Age = 30,
IsStudent = true
};
// Serialize the object to JSON
string json = JsonSerializer.Serialize(obj);
// Output the JSON
Console.WriteLine(json);
return json;
}
}
}
Using ‘Newtonsoft.Json’
using Newtonsoft.Json;
using System;
namespace ConsoleApplication1
{
public class ObjectToJSON
{
public static void Main(string[] args)
{
ObjectToJSON.Convert();
Console.ReadLine();
}
public static string Convert()
{
// Define an object
var obj = new
{
Name = "John",
Age = 30,
IsStudent = true
};
// Serialize the object to JSON
string json = JsonConvert.SerializeObject(obj);
// Output the JSON
Console.WriteLine(json);
return json;
}
}
}
Explanation
In both examples, an anonymous type is used to define the object to be serialized. You can change it with any custom class or object you want to convert to JSON.
JsonSerializer.Serialize method (from System.Text.Json) or
JsonConvert.SerializeObject method (from Newtonsoft.Json) is used to convert the object to JSON format.
The resulting JSON string is then printed to the console.
Library Selection
System.Text.Json is part of the .NET Core and .NET 5+ frameworks, and it provides high-performance JSON parsing and serialization.
Newtonsoft.Json (JSON.NET) is a popular third-party library with extensive features and flexibility. It has been widely used in the .NET ecosystem for many years and provides excellent support for various scenarios.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
You can convert an object to JSON using the built-in
System.Text.JsonorNewtonsoft.Json(also known as JSON.NET) libraries. Both libraries provide methods to serialize C# objects into JSON format.Here's how you can do it using both libraries:
Using ‘System.Text.JSON’
Using ‘Newtonsoft.Json’
Explanation
JsonSerializer.Serializemethod (fromSystem.Text.Json) orJsonConvert.SerializeObjectmethod (fromNewtonsoft.Json) is used to convert the object to JSON format.Library Selection
System.Text.Jsonis part of the .NET Core and .NET 5+ frameworks, and it provides high-performance JSON parsing and serialization.Newtonsoft.Json(JSON.NET) is a popular third-party library with extensive features and flexibility. It has been widely used in the .NET ecosystem for many years and provides excellent support for various scenarios.