1

How do I convert a POJO to a GSON JsonObject?

class MyPojo { String name; String id; } MyPojo pojo = new MyPojo(); pojo.name = "Roger"; pojo.id = "1"; Gson gson = new Gson(); gson.toJson(pojo); // Problem is this returns a string instead of a JsonObject 

1 Answer 1

4
JsonObject json = gson.toJsonTree(pojo).getAsJsonObject(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.