In this tutorial, we’ll explore how to convert an object to JSON without escape characters in C#.NET. Escape characters are characters that are used to represent certain special characters in string literals.
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is often used for transmitting data between a web server and a client, as it is a text format that is language-independent.
By default, when you serialize an object to JSON in C#, it escapes characters that have special meaning in JSON syntax. For example:
string jsonString = JsonConvert.SerializeObject("He said, \"Hello World!\"");
Console.WriteLine(jsonString);
Output:
"He said, \\\"Hello World!\\\""
In the output above, you can see that the double quotes are escaped.
Removing Escape Characters
Sometimes, you may want to serialize an object without escape characters. To do this, you can use the StringEscapeHandling
option provided by JsonConvert
. Let’s walk through the steps to achieve this.
Step 1: Add the required using directive
using Newtonsoft.Json;
Note: To use JsonConvert
, you need to have the Newtonsoft.Json package installed. You can install this through NuGet Package Manager with the command Install-Package Newtonsoft.Json
.
Step 2: Define your object
Create an object that you want to serialize to JSON. In this example, we’ll use a simple class:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Bio { get; set; }
}
Step 3: Serialize the object without escape characters
To serialize the object without escape characters, you should set StringEscapeHandling
to EscapeHtml
.
Person person = new Person
{
Name = "Tom Urtis",
Age = 30,
Bio = "Tom said, \"I love coding in C#\""
};
JsonSerializerSettings settings = new JsonSerializerSettings
{
StringEscapeHandling = StringEscapeHandling.EscapeHtml
};
string jsonString = JsonConvert.SerializeObject(person, settings);
Console.WriteLine(jsonString);
Output:
{"Name":"Tom Urtis","Age":30,"Bio":"Tom said, "I love coding in C#""}
Here is the complete code:
using System;
using Newtonsoft.Json;
namespace JsonSerializationExample
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Bio { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Define your object
Person person = new Person
{
Name = "Tom Urtis",
Age = 30,
Bio = "Tom said, \"I love coding in C#\""
};
// Define serialization settings
JsonSerializerSettings settings = new JsonSerializerSettings
{
StringEscapeHandling = StringEscapeHandling.EscapeHtml
};
// Serialize the object to JSON without escape characters
string jsonString = JsonConvert.SerializeObject(person, settings);
// Output the JSON string to the console
Console.WriteLine(jsonString);
}
}
}
Conclusion
In this tutorial, we have learned how to serialize an object to JSON without escape characters in C#. This can be particularly useful when you want to have a more human-readable output or when you need to process JSON strings without escape characters.
You may also like:
- get the first n characters of a string in c#.net
- Difference Between String and StringBuilder in C#.NET
- remove a particular character from string C#.net
- How to create an array in asp.net using c#.net?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…