import java.util.HashMap;
public class JSON {
public String name;
public HashMap<String, String> Credentials = new HashMap<String, String>();
public JSON(String name){
Credentials.put(name, name);
}
}
JSON json = new JSON("Key1");
new Gson().toJson(json);
I get the following value as output.
{“Credentials”:{“Key1″:”Key1”}}
Now how would i create an JSONObject something like this below using Gson.
Building on what @Brian is doing, you just need the auto serialization piece.
What you do is the following, and I have to state, this is with regard to a single object at the moment. You’ll have to look through the GSON documentation for more detail on this if you’re dealing with a collection of objects at the top level.
Hopefully that will give you enough context to work with. Gson should be smart enough to figure out your properties and give them sensible names in the output.