Convert Java object to JSON with Gson (generating with backslash)

Asked

Viewed 1,026 times

1

I am trying to generate a JSON based on a Java object.

JSON is being generated, but some bars have been included improperly:

import java.io.Serializable;

import com.google.gson.annotations.SerializedName;

public class Documento implements Serializable{
    private static final long serialVersionUID = 1L;

    @SerializedName("Identificador")
    private String identificador;

    public Documento (){}

    public Documento (String identificador){
        this.identificador = identificador;
    }

    public String getIdentificador() {
        return identificador;
    }

    public void setIdentificador(String identificador) {
        this.identificador = identificador;
    }
}

String identificador = jsonObjResposta.getAsJsonArray("ListaObjetos").iterator().next().getAsJsonObject().get("Identificador").toString();

Documento documento = new Documento(identificador);

System.out.println("gson.toJson(documento): " + gson.toJson(documento));

Exit:

gson.toJson(documento): {"Identificador":"\"bene1\""}

OBS: I saw that in fact the identificador obtained from jsonObjResposta is coming with the quotes, but I can’t remove it.

  • Seixas, it seems that he is performing the "escape" of the string. Try to instantiate the object in this way and validate if it worked Gson gson = new GsonBuilder().disableHtmlEscaping().create();

  • I did a test here and it ran out of bar: {"Identificador":"bene1"}. I used version 2.8.5 of Gson and created it with new Gson(). Could [Edit] the question putting as the object Gson is created and/or some other configuration that you are using? Also see if it is this document that causes the problem, because \" would only be used if the identifier had quotes on it.

  • Joãomanolo and hkotsubo, thanks for the immediate return. I changed the question, after testing with a string, it worked. But I’m creating the new object with information obtained from another json, in which case it doesn’t work. Could you help me? Thanks!!!

  • Well, what is the value of the string identificador? 'Cause I think she’s coming with the quotes - guess, because you didn’t say what’s in the jsonObjResposta :-) A general tip is to think that other people here do not have the same context as you (which is the only one that has the overview of your system). We only know what you tell us, and you do not say what is in the variables, we can only guess... Anyway, my guess is that the identificador is coming in quotes (print its value to check)

  • @hkotsubo, you’re correct, I’m sorry. The string is recovered with quotation marks, I’m not able to change the quotation marks with replace, I’ll have to go through every string even to replace?

  • To remove all quotes, just do identificador = identificador.replaceAll("\"", "") (will remove all quotation marks, which seems to be the case)

  • I apologize to the friends who promptly helped me by initially posting the incorrect question. @hkotsubo, perfect, your solution solved my problem. Post your answer so I can mark as solution. Thank you all. Thanks a lot!!!

  • @Seixas I only edited the question so that my answer makes a little more sense and does not look like "divination". If you disagree with the edition, just undo it :-)

  • @hkotsubo Show!!! It’s perfect!!!

Show 4 more comments

1 answer

2


I did a basic test with version 2.8.5 of Gson:

Gson gson = new Gson();
Documento documento = new Documento("bene1");

System.out.println("gson.toJson(documento): " + gson.toJson(documento));

The way out was without the quotes:

gson.toJson(documento): {"Identificador":"bene1"}

Fur comments we saw that in fact the String original was coming with quotes, so a solution would simply remove them, using replaceAll:

// remove as aspas do identificador
identificador = identificador.replaceAll("\"", "");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.