Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson.toJson object contain URL

Tags:

json

gson

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

like image 532
gpa Avatar asked Jun 22 '12 03:06

gpa


People also ask

What does GSON toJson do?

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.

What is difference between fromJson and toJson?

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.

Is GSON toJson thread safe?

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.


1 Answers

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.

like image 87
Jesse Wilson Avatar answered Sep 26 '22 11:09

Jesse Wilson