I have the ungrateful task to build a JSON String in Java, manually, without any framework, just a StringBuilder. I know this is bad, but it is only part of a prototype, I will do this better next time.
My question: How can I put the String -> "Some text WITH quotes" <- into a JSON object?
Of course, {"key" : " "Some text WITH quotes" "} is no valid json because of the unescaped quotes.
I think I have to use String.replace here, but by what can I replace the quotes? Same question for the slash “/”. What is the proper replacement?
Thank you
translates to:
You can use
StringEscapeUtils.escapeJavaScript()in apache-commons Lang 2.6 orStringEscapeUtils.escapeEcmaScript()in 3.4 to do the hard-work of escaping for you.