Monday, 12 August 2013

How to escape double quotes in JSON using Java

How to escape double quotes in JSON using Java

What is the best way to handle double quotes that exist inside a JSON
String ? I get the string from an external source. Escaping the double
quotes would escape all the double quotes, even the ones that wrap around
the key and the value. I would ideally just want a way to escape the
quotes inside the value. In the example below, the string "quotes" breaks
the JSON. What would be the best way to handle this ? Appreciate your
input. Thanks!
@Test
public void isValidJSON()
{
String testJSONString = "{\"messageType\":\"typeA\",
\"subject\":\"Here is a subject with \"quotes\" in it\" }";
try
{
new JSONObject(testJSONString);
} catch (JSONException e)
{
e.printStackTrace();
}
}
The error I get is:
org.json.JSONException: Expected a ',' or '}' at 60 [character 61 line 1]

No comments:

Post a Comment