I noticed that if I put double quotes on my input boxes that get passed to php via json, json_decode will return null. Thus, I decided to replace double quotes with \" when making my json.
Thus, I have something like:
var valueToSet = input.replace(/"/gi, '\\"');
The code above replaces "" with \" … This works, and now I can input stuff like:
Hello hi "" " " " ' ' '' ' ' ' """
without breaking anything. However, if I input something like:
Hello hi "" \ "
it gets broken. How can I include to the above replace that slashes get also replaced? And what should I replace slashes with? with \\ ?
Thanks!
You should not worry about these issues. I think you’re buiding your JSON by hand, while you should use a dedicated function that will take care of those details for you.
JSON.stringifymethod that will work in all browsers and will use the native implementation if present.JSON.stringify(IE from version 8.0, Firefox from version 3.5, Chrome from version 4.0, Safari from version 4.0, Opera from version 10.5)json_encodeTo answer your last question, yes,
\should be replaced with\\, but again, it should be done byjson_encode.