简体   繁体   中英

JSON.parse can't recognize \" (backslash and quote) in string to parse

I have serialized JSON with objects that have quotes in string like this:

var bla = [
    { 
        "label": "person", 
        "id": "1", 
        "data": "bla means: \"lala\"" 
    }
];

I know how it looks on server side. But when I debug it in browser I see that I receive error on this because it looks like below:

var bla = [
    { 
        "label": "person", 
        "id": "1", 
        "data": "bla means: "lala"" 
    }
];

and my error :

Uncaught SyntaxError: Unexpected token l in JSON at position 25 at JSON.parse ()

I read that JSON should recognize \\" https://www.json.org/

Any ideas where should I correct it? On server side when I serialized object or on client side with JavaScript?

EDIT:

On server side I use C# library "System.Web.Script.Serialization" The object to serialize:

List<SomeObject> someObject= new List<SomeObject>();
        SomeObject bla1 = new SomeObject();
        bla1.id = "1";
        bla1.label = "person";
        bla1.label = "bla means: \"lala\"";
        someObject.Add(bla1);

var bla = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(someObject);

SUMMARY

I fix in on serialized string on server side, because I use API where I can't manipulate this objects. var bla = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(someObject).Replace("\\\\\\"", "\\\\\\\\\\\\\\"");

You can try this with double backslash or triple backslash like \\\\" or \\\\\\"

var bla = [
    { 
        "label": "person", 
        "id": "1", 
        "data": "bla means: \\"lala\\"" 
    }
];

It will add backslash and print it with double quotes.

The server should serve proper serialized JSON that can be parsed directly, without the client having to do tricky string manipulations on the data first. (For example, if you don't fix it server-side, then it becomes invalid to fetch(...).then(res => res.json()) )

I didn't fully understand your project's structure, but:

backslash \\ is a special character in javascript.

It is special because it has a regex special meaning.

  1. When writing \\hello , your output would be hello
  2. When writing \\'hello , your output would be 'hello .
  3. When writing \\"hello\\" , your output would be "hello"

I think you should first choose how exactly you want your data property to be formatted.

Do you want data to be a string? array? object? a string which keeps all of its quotation marks as double or single?

Have a look here - https://www.w3schools.com/js/js_strings.asp

And decide the string format you want to work with.

If you want to escape a character (which actually means to have it in your string), you should add \\ before this character.

In my case, I just use String.raw() .

JSON.parse(`{ "key": "\""}`) // fail
JSON.parse(String.raw`{ "key": "\""}`) // work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM