I am using Gson.toJSON method. My pojo contains one of attribute as URL string. Strange thing is Gson converter changes URL characters
output is: /myApp/myAction.html?method\u003drouter\u0026cmd\u003d1
expected output is: /myApp/myAction.html?method=router&cmd=1
Introduction. Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.
There are two parameters in the fromJson() method, the first parameter is JSON String which we want to parse and the second parameter is Java class to parse JSON string. We can pass one parameter into the toJson() method is the Java object which we want to convert into a JSON string.
Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it. Gson instances are Thread-safe so you can reuse them freely across multiple threads.
Create your Gson instance like this:
Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.create();
Disabling HTML escaping will prevent GSON from encoding the space character as \u0026.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With