Viral Shah
Viral Shah

Reputation: 2938

Gson toJson method includes escape character when printing Strings

I'm using Gson to convert a Java object to Json. One of the object's fields holds a string containing an escaped double quote, like this:

"double quote:'\"'"

The toJson method returns the string as above, but I would like to print this instead:

double quote:'"'

Is this possible using Gson?

Upvotes: 1

Views: 3452

Answers (1)

BalusC
BalusC

Reputation: 1108712

Why would you want to do that? The surrounding quotes and the escape character are mandatory in JS/JSON as well.

The following JS piece just shows the correct value:

var json = { test: "double quote:'\"'" };
alert(json.test);

Don't print it using System.out.println() or so, Java doesn't parse JSON, only JS does that ;)

Upvotes: 1

Related Questions