title: How to convert automatically list of objects to JSONArray with Gson library in java?
body: <p>Lets say I have following list of custom object: <code>ArrayList&lt;GroupItem&gt;</code> where <code>GroupItem</code> is some class that has <code>int</code> and <code>String</code> variables. </p>
  
  <p>I tried so far <code>Gson</code> library but it's not exactly what I want.</p>
  
  <pre><code>ArrayList&lt;GroupItem&gt; groupsList = /* ... */
  Gson gson = new Gson();
  JsonElement element = gson.toJsonTree(groupsList, new TypeToken&lt;ArrayList&lt;GroupItem&gt;&gt;() {}.getType());
  
  JsonArray jsonArray = element.getAsJsonArray();
  </code></pre>
  
  <p>My goal is to get <code>JSONArray</code> (org.Json.JSONArray) somehow. Any ideas?</p>
  
  <p><strong>[EDIT]</strong></p>
  
  <p>My project is Android with Cordova that has API based on <code>org.Json.JSONArray</code></p>
  
  <p>ANd I thought to write some generic way to convert instances to <code>JSONArray</code> / <code>JSONObject</code> </p>
  
  <p>Thanks,</p>