0

I am trying to build json string from my java object. This object contains string with '&' and '='. Now after calling gson.toJson my string changes from those charecters to \u003d and \u0026.

Example:

    public static void main(String[] args) {
    MyObj myObj = new MyObj();
    myObj.setUrl("www.koko.com?k=1&v=2");
    Gson g = new Gson();
    System.out.println(g.toJson(myObj));

}

public static class MyObj{
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

OUTPUT:

{"url":"www.koko.com?k\u003d1\u0026v\u003d2"}

Expecting:

{"url":"www.koko.com?k=1&v=2"}

Any suggestions how to avoid this behavior ?

Thanks

Ehud Lev
  • 2,461
  • 26
  • 38

1 Answers1

1

https://stackoverflow.com/a/4147245/7557573

You need to disable HTML escaping.

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

Yuriy Alevohin
  • 941
  • 7
  • 18