so I have the following line of javascript:

YAHOO.lang.JSON.parse(txt)

where text is a string that is pulled from the database. This is an example of one of the problem strings I'm getting back:

5000\25\30%

The JSON parser is throwing syntax errors on the / character as far as I can tell. I looked through threads here and most of them are saying to change it to "//" but I'm pulling hundreds of values from a database so I can't change their source. I'm trying to get it to replace the '/' char with just the empty string since I don't actually have to display the '/', but I'm having difficulties. My current replace code looks like this:

if(substr.indexOf("\\") > 0){

  substr.replace(/\\/g , "");

 }

but nothing is happening. Sometimes the "indexOf" check fails, so I tried running the replace on every string and it still didn't replace any of the '\' chars. So here is my question: If I get back a string with one or more '\' characters and need to remove them for a JSON parser using javascript, how do I do that; or how do I get a JSON parser to accept the '\' char without changing the source string.

Edit: I think what might be the problem is that "\2" and all the other ones are evaluating and exceping characters that don't do anything. These characters are always escaped so they cannot be replaced. Is this even possible to fix the error?

link|flag
This sounds like looking for a solution for a problem that shouldn't be there in the first place. What does txt contain? Who is throwing errors at which point with what message? – Pekka Oct 5 at 15:43
txt contains a string that is retrieved from the database which is a bunch of key:value pairs in the form "NAME":"value" where the value are strings. it works for all strings except for the ones that happen to have a '\' character. To figure out what was going wrong I manually pulled out each string and pushed though to the JSON parser, when I did this the only strings that failed are the ones with a '\' in them. – Drew Oct 5 at 15:57
@Drew I think you need to show some code. How do you retrieve the data from the database? – Pekka Oct 5 at 16:01
It is not the retrival to my knowledge, the same code works perfectly fine when I retrieve a different set of key:value pairs; but this is how I'm doing it: var xhr = AjaxMsg.prototype.getXMLHTTPRequest(); xhr.open("GET", url, false); xhr.send(null); var txt = xhr.responseText; – Drew Oct 5 at 16:03
I'd be worried about a database that contains “JSON” strings with raw backslashes in. What happens to the sequence \"? – bobince Oct 5 at 16:17

1 Answer

I was able to figure this out. What it took was changing the jsonWrite that was creating the inital return from the database and doing a replaceAll("\x5C", "\"), replacing the character with the html code for it so it still displays and is able to be parsed.

link|flag

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.