public String toJson() {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
return gson.toJson(this);
}
This method converts a class into a JSON string. But I want to convert a class into a JsonElement. Is this directely possible or do I have to parse the string again?
You can use toJsonTree method of Gson class
Gson gson = new GsonBuilder().create();
JsonElement element = gson.toJsonTree(object,typeOfObject);
Reference javadoc
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