I’m using Gson API and created a class like:
public class Person
{
private String name;
private JsonObject someInfo;
public Person()
{
}
}
When I serealize an instance of this class using:
Person person = new Person();
person.name = "Charles"
person.someInfo = new JsonObject();
person.someInfo.addProperty("key1","value1");
Gson gson = new Gson();
String jsonStr = gson.toJson(person);
I get the info inside a members properties:
{"members":{"name":"Charles","someInfo":{"key1":"value1"}}}
I’ve noticed that if I declare the Person’s class someInfo global variable as JsonElement instead of JsonObject the info is displayed correctly.
Is there any way to serealize the information using JsonObject?
What I expect is:
{"name":"Charles","someInfo":{"key1":"value1"}}
Use a Map for your someInfo:
Although it would be better to use proper getters/setters to access
Personvariables.