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:
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
How to Set CSS to Add an Asp Img
Why Does a Float Variable Stop Incrementing at 16777216 in C#
Why Can't I Retrieve an Item from a Hashset Without Enumeration
Entity Framework 4.1 Inverseproperty Attribute and Foreignkey
How to Remove Specific Style Tag in HTML Using C#
C# List - Removing Items While Looping/Iterating
How to Get the Selected Item of a Combo Box to a String Variable in C#
Why Is ASP.NET Identity Identitydbcontext a Black-Box
How to Convert Style Text to C# Object Such as Class/Hashtable/Collection
Webbrowser Control - No CSS Applied
File Exists by File Name Pattern
How to Determine The Screen Width/Height Using C#
Why Does "Dtoa.C" Contain So Much Code