Convert a JSON object into a string in C#

You can convert a string to a JSON object in C# by using the JsonSerializer.Deserialize method from the System.Text.Json namespace.

Here’s an example:

using System;
using System.Text.Json;

namespace CSharpMemberApp
{
    public class CSharpMember
    {
        public string Name { get; set; }
        public string Bio { get; set; }
        public DateTime JoinDate { get; set; }
        public bool Author { get; set; }
    }

    public class Program
    {
        public static void Main()
        {
            CSharpMember member = new CSharpMember
            {
                Name = "John Doe",
                Bio = "Software developer",
                JoinDate = DateTime.Now,
                Author = true
            };

            string jsonString = JsonSerializer.Serialize(member);
            Console.WriteLine(jsonString);
        }
    }
}

If you run this code, it will create a new CSharpMember object, set its properties to the specified values, and then serialize it into a JSON string and print it to the console. The output will look like this,

{"Name":"John Doe","Bio":"Software developer","JoinDate":"2023-05-31T20:29:33-04:00","Author":true}


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.