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();
– João Manolo
I did a test here and it ran out of bar:
{"Identificador":"bene1"}
. I used version 2.8.5 of Gson and created it withnew Gson()
. Could [Edit] the question putting as the objectGson
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.– hkotsubo
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!!!
– Seixas
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 thejsonObjResposta
:-) 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 theidentificador
is coming in quotes (print its value to check)– hkotsubo
@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?
– Seixas
To remove all quotes, just do
identificador = identificador.replaceAll("\"", "")
(will remove all quotation marks, which seems to be the case)– hkotsubo
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
@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
@hkotsubo Show!!! It’s perfect!!!
– Seixas