Code Spaces in JSONObject

I am creating a JSON string to send to my web service. Since one of the parts is entered by the user, there is the possibility of double quotes. I am trying to solve a problem by avoiding it.

String strValue = "height of 6\""; JSONObject json = new JSONObject(); json.put("key", strValue.replaceAll("\"","\\\"")); 

The problem here is when I do json.toString() , I get 3 slashes.

Example:

 {"key","height of 6\\\""} 

If I'm not trying to do a replacement, json.toString() gives me broken json.

Example:

 {"key", "height of 6""} 

How can I do it right?

Note. When my site saves this value and displays it, it displays height of 6\"

UPDATE:

The culprit seems to be json.toString()

When I call the replaceAll method, it - correctly - only escapes the double quote. It looks like json.toString() slashes. To solve the problem, I have to do json.toString().replace("\\\\", "") . This begs the question: why on Earth does JSONObject execute a slash, not double quotes ?????

+6
source share
1 answer

The culprit seems to be json.toString()

When I call the replaceAll method, it - correctly - only escapes the double quote. It looks like json.toString() slashes. To solve the problem, I have to do json.toString().replace("\\\\", "") .

This begs the question: why on Earth does JSONObject execute a slash rather than double quotes ?????

+2
source

All Articles