How can I encode JSON object content into a JSON string?

Viewed 32923

Trying to find a way to stringify a JSON string, with escape characters like JavaScript.

For example --

Input:

{"name":"Error","message":"hello"}

Output:

"{\"name\":\"Error\",\"message\":\"hello\"}"

I am able to get the object as JSON String using Gson, but not stringify (with escape characters).

Is this possible with Java?

3 Answers
Related