When you execute the JavaScript code:
JSON.parse(“\”This is a unicode character: \u0007\””)
in a browser you will get the error:
Uncaught SyntaxError: Unexpected token in JSON at position 30
at JSON.parse (<anonymous>)
at <anonymous>:1:6
I fixed this by altering the C# code that returned the JSON.
public
static
class
MvcHelper
{
public
static
ActionResult ConvertToJsonActionResult(object data)
{
var content = ConvertToCamelCaseJsonString(data);
content = RemoveEscapedUnicodeCharactersFromString(content);
return ConvertStringToJsonContentResult(content);
}
private
static
string ConvertToCamelCaseJsonString(object data)
{
var serializationSettings =
new
JsonSerializerSettings
{
ContractResolver = new
CamelCasePropertyNamesContractResolver()
};
return
JsonConvert.SerializeObject(data, serializationSettings);
}
private
static
ContentResult ConvertStringToJsonContentResult(string content)
{
return
new
ContentResult()
{
Content = content,
ContentType = “application/json”,
ContentEncoding = System.Text.Encoding.UTF8
};
}
private
static
string RemoveEscapedUnicodeCharactersFromString(string content)
{
var regex = new System.Text.RegularExpressions.Regex(@”\\+[uU]([0-9A-Fa-f]{4})”);
return regex.Replace(content, string.Empty);
}
}
Then you can convert your C# ViewModel to a Json ActionResult:
[HttpPost]
public
ActionResult WizardData(int taakId)
{
var model = new
MyViewModelViewModel();
return
MvcHelper.ConvertToJsonActionResult(model);
}