JSON Parsing error with backslash

c#, javascript, json, json.net

Solution

The best way to inject JSON into JavaScript source code (when generating the JavaScript with a server side language), is to inject it directly where you need it, not inside a string literal that needs to be parsed.

For example:

var foo = <json blob>;

so that the result will be

var foo = {"World":"Hello\\Test"};

JavaScript will interpret this as an object literal resulting in an object, which is what you want to get anyway. This avoids all the problems with "nested" escape sequences.

Problem

I have been trying to figure out why the following JSON input will be failed to parse in JSON.parse function. ``` {"World":"Hello\\Test"} ``` The json above is being returned by JSON.NET. I have tried multiple methods to get this working. As you can see the backslash is escaped and https://jsonlint.com/ is able to parse it. I have a failing sample at https://jsfiddle.net/ckp0uc0p/3/ as well. Any help will be appreciated.

Original source

Related problems