Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Create JSON String in C#

Date- May 31,2023

4524

Certainly! Here's an example of how you can create a JSON string in C# using the Newtonsoft.Json library:

using Newtonsoft.Json;

class Program
{
    static void Main()
    {
        // Create an object to be serialized to JSON
        var person = new Person
        {
            Name = "John Doe",
            Age = 30,
            Address = "123 Main St",
            City = "New York"
        };

        // Convert the object to a JSON string
        string jsonString = JsonConvert.SerializeObject(person, Formatting.Indented);

        // Output the JSON string
        Console.WriteLine(jsonString);
    }
}

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
}

In this example, we define a Person class with some properties. We then create an instance of the Person class with some sample data. Using the JsonConvert.SerializeObject() method from the Newtonsoft.Json library, we convert the person object to a JSON string. Finally, we output the JSON string to the console.

Make sure to include the Newtonsoft.Json namespace by adding the following line at the top of your C# file:

using Newtonsoft.Json;
You'll also need to have the Newtonsoft.Json NuGet package installed in your project. You can do this by right-clicking on your project in Visual Studio, selecting "Manage NuGet Packages," and searching for "Newtonsoft.Json" to install it.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page