title: Create JSON object in Java8 using com.google.code.gson
body: <p>I am trying to create a JSON Object using <code>com.google.code.jso</code> in JAVA 8. I have a list of objects and i am iterating through them. The object is as:</p>
  
  <pre><code>public class MyObject {
      private String name, status, cause, id;
  
      public String getName() {
          return name;
      }
  
      public String getStatus() {
          return status;
      }
  
      public String getCause() {
          return cause;
      }
  
      public String getId() {
          return id;
      }
  
  }
  </code></pre>
  
  <p>I have a list of above objects and i am trying to convert them to JSON using the below (the relevant code):</p>
  
  <pre><code>import com.google.gson.JsonObject;
  
  JsonObject status = new JsonObject();
  for (MyObject obj : objectLists){
              status.add(obj.getId(),
                      new JsonObject()
                              .add("Name: ", obj.getName())
                              .add("Status: ", obj.getStatus())
                              .add("Cause: ", obj.getCause())
              );
          }
  </code></pre>
  
  <p>I was hoping to get a JSON of the following form:</p>
  
  <pre><code>{
  
    "0 (this is the id i get from myObject.getId())": {
      "name": The name i get from myObject.getName(),
      "Status": The status from myobject.getStatus(),
      "cause": The status from myobject.getCause()
    },
   "1": {
      "name": "myname",
      "Status": "mystatus",
      "cause": "cause"
    }
  }
  </code></pre>
  
  <p>So I have 2 question.</p>
  
  <ol>
  <li><p>I am getting an error in creating the Json object. <code>Wrong 2nd
  argument type. Found: 'java.lang.String', required:
  'com.google.gson.JsonElement'</code> I understand that i have to change
  the second parameter but i could not find in the docs how to do
  that.</p></li>
  <li><p>How can i pretty print this.</p></li>
  </ol>
  
  <p>Thanks </p>