Raunak Raunak - 1 year ago 120
Java Question

Creating GSON Object

How do I create a json Object using Google Gson?
The following code creates a json object which looks like


JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "john");

How do I create a jSon Object like this one?


Answer Source

Figured it out how to do it correctly using Java Objects.

Creator creator = new Creator("John");
new Gson().toJson(creator);

Implementation of Creator java class.

public class Creator {

    protected String name;

    protected HashMap<String, String> publisher = new HashMap<String, String>();

    public Creator(String name){
            publisher.put("name", name);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download