Serializing Strings Containing Apostrophes with JSON.Net

Serializing strings containing apostrophes with JSON.Net

Though there are some cases wherein you might want to drop some JSON into your page as a JavaScript string, or an HTML attribute value, most often what you'd do is simply include it directly into JavaScript source, because JSON is valid JavaScript syntax after all.

Deserialize json string that contains singlequote using c#

For a start, it might be worth noting that the JSON syntax uses single quotes where you have used double quotes. Here is a guide for proper syntax:

JSON Syntax

Now unfortunately JSON does not allow the use of single quotes like that, but we can use the unicode \u0027 for an apostrophe and make use of JSON's serializer settings, as you have already done. So your original string:

string json = "{\"PostData\": '{\"LastName\": \"O' Corner\",\"FirstName\":\"Mark\",\"Address\":\"123 James St\"}'}";

becomes:

string json = "{'PostData': {'LastName': 'O\u0027 Corner','FirstName':'Mark','Address':'123 James St'}}"

This is assuming that you are parsing a string literal, otherwise you would need to escape the unicode to give:

string json = "{'PostData': {'LastName': 'O\\u0027 Corner','FirstName':'Mark','Address':'123 James St'}}"

Getting an error while doing Newtonsoft JSON Deserialize for apostrophe strings in C#

That's a single quote, not an apostrophe so it needs to be escaped :

string json1 = @"{'Name':'Jame\'s'}";

You can't include the string delimiter inside a string without escaping it.

Tell Json.Net to write a single-quote rather than a double quote when serializing objects

Yes, this is possible. If you use a JsonTextWriter explicitly instead of using JsonConvert.SerializeObject(), you can set the QuoteChar to a single quote.

var obj = new { key = "value" };

StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
writer.QuoteChar = '\'';

JsonSerializer ser = new JsonSerializer();
ser.Serialize(writer, obj);
}

Console.WriteLine(sb.ToString());

Output:

{'key':'value'}

Fiddle: https://dotnetfiddle.net/LGRl1k

Keep in mind that using single quotes around keys and values in JSON is considered non-standard (see JSON.org), and may cause problems for parsers that adhere strictly to the standard.

C# - Having trouble escaping an apostrophe for JSON

You are mixing semantics. C# and javascript with double quotes use backslash as escape character like C, javascript with single quotes or C# with a prepended @ are literals.

When you set:

reportParameters = '{ "Location" : "Billy\'s House" }';

you are using literals, so the backslash is being included in the string. When you see the string in C# you see it escaped, as the backslash is the escape character it's escaped with another backslash character, but that doesn't means there are two backslases, there's only one, but the debugger in C# shows you it in it's escaped form.

If you want to compose correctly the string in javascript use this:

reportParameters = "{ \"Location\" : \"Billy's House\" }";


Related Topics



Leave a reply



Submit