Why doesn't my attempt to escape quotation mar

2019-07-09 05:36发布

问题:

I am parsing a JSON-string with the JQuery.parseJSON function, as I have done lot's of times in my code. On this particular case, though, I get: Uncaught SyntaxError: Unexpected token R. The only upper case R that exists, in my JSON-formatted String, comes right after an escaped quotation mark, ... \"R ... like this. It seems like too much of a coincidence to be caused by anything other than this, but as far as I can tell, I have completely followed the proper syntax as described on json.org.

EDIT:

I've tried to manually remove the occurrances of \" in a hardcoded test, and the string formats perfectly into a proper Javascript object. In other words, my \" is definitely the problem here...

var myObject = $.parseJSON(myString);

EDIT 2:

the problematic area of my String is here displayed, both in working, and not working condition. first the problematic one:

{"lineID":33,"boxID":10,"title":"My text with the \"Ruining Part\""}

Then the working one:

{"lineID":33,"boxID":10,"title":"My text with the Ruining Part"}

Finally how i format my javabean object into JSON string.

String jsonObjectAsString = new Gson().toJson(myJavaBeanObject);

回答1:

You probably need to escape the backslash in your string, if it is hardcoded, so that the final string that gets parsed has a single backslash followed by a double quote. Otherwise, the browser thinks you are trying to escape a double quote in your string, which does nothing.

So change your string to:

...\\"R...