-1

I convert json string to JSONObject using JSONObject jsonObject = new JSONObject(jsonString); And the convert jsonObject to Java Object using gson

But it gives an error when a json attribute value contains quotes, like , { "length" : "10"" } (its, 10 inches)


Edit : i get data from server api in following manner :
"{\"data\":\"10\"\"}"
i replace \" by ", and which converts into { "data" : "10"" } this gives exception as it cannot convert jsonobject into java object
org.json.JSONException: Unterminated object at character 13 of {"data":"10""}
How can i convert "{\"data\":\"10\"\"}" to { "data" : "10\"" }


Edit 2 : I was converting the string to JSONObject the wrong way.
By removing slashes, I was also removing the slashes of escaped characters.

Solution : Instead, I used StringEscapeUtils.unescapeJson(jsonString) which didn't remove the slashes of escaped characters inside json key-value data.

Abhishek Aggarwal
  • 207
  • 2
  • 4
  • 13

3 Answers3

2

Please share your error as well. As far as from your Question i can see there are 2 quotes near 10.

{ "length" : "10"`"` }                    

If you want to insert " for inches please provide \".

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Nizamudeen Sherif
  • 1,002
  • 1
  • 12
  • 38
2

You have to use \ escape character before ". Here's what you should do:

{ "length" : "10\"" }

If you have to use special character in your JSON string, you can escape it using \ character.

See this list of special character used in JSON :

\b  Backspace (ascii code 08)
\f  Form feed (ascii code 0C)
\n  New line
\r  Carriage return
\t  Tab
\"  Double quote
\\  Backslash character
iamgopal
  • 634
  • 5
  • 12
0

Try this

jsonString = "{\"data\":\"10\"\"}";

String jsonFormattedString = jsonString.replaceAll("\\\\", "");

Log.i("Response",":"+jsonFormattedString);

OUTPUT

I/Response: :{"data":"10""}

EDIT

jsonString = "{\"data\":\"10\"\"}";

String jsonFormattedString = downloadDetails.replaceAll("\"", "");

Log.i("Response",":"+jsonFormattedString);

JSONObject jsonObject = new JSONObject(jsonFormattedString);
String data = jsonObject.getString("data");
Log.i("Response data",":"+data);

OUTPUT

I/Response: :{data:10}
I/Response data: :10

LAST EDIT

jsonString = "{\"data\":\"10\"\"}";

String jsonFormattedString = downloadDetails.replaceAll("\"", "");

Log.i("Response",":"+jsonFormattedString);


final String regex = "([a-zA-Z0-9-]+):([a-zA-Z0-9-]+)";
final String subst = "\"$1\":\"$2\"";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(jsonFormattedString);

jsonFormattedString = matcher.replaceAll(subst);

Log.i("Final Response",":"+jsonFormattedString);

JSONObject jsonObject = new JSONObject(jsonFormattedString);
String data = jsonObject.getString("data");
Log.i("Response data",":"+data);

data = data+"\"";

Log.i("FINAL VALUE",":"+data);

LAST OUTPUT

I/Response: :{data:10}
I/Final Response: :{"data":"10"}
I/Response data: :10
I/FINAL VALUE: :10"
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31