title: convert ArrayList<MyCustomClass> to JSONArray
body: <p>I have an ArrayList that I use within an ArrayAdapter for a ListView.  I need to take the items in the list and convert them to a JSONArray to send to an API. I've searched around, but haven't found anything that explains how this might work, any help would be appreciated.</p>
  
  <p><strong>UPDATE - SOLUTION</strong></p>
  
  <p>Here is what I ended up doing to solve the issue.</p>
  
  <p>Object in ArrayList:</p>
  
  <pre><code>public class ListItem {
      private long _masterId;
      private String _name;
      private long _category;
  
      public ListItem(long masterId, String name, long category) {
          _masterId = masterId;
          _name = name;
          _category = category;
      }
  
      public JSONObject getJSONObject() {
          JSONObject obj = new JSONObject();
          try {
              obj.put("Id", _masterId);
              obj.put("Name", _name);
              obj.put("Category", _category);
          } catch (JSONException e) {
              trace("DefaultListItem.toString JSONException: "+e.getMessage());
          }
          return obj;
      }
  }
  </code></pre>
  
  <p>Here is how I converted it:</p>
  
  <pre><code>ArrayList&lt;ListItem&gt; myCustomList = .... // list filled with objects
  JSONArray jsonArray = new JSONArray();
  for (int i=0; i &lt; myCustomList.size(); i++) {
          jsonArray.put(myCustomList.get(i).getJSONObject());
  }
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>[{"Name":"Name 1","Id":0,"Category":"category 1"},{"Name":"Name 2","Id":1,"Category":"category 2"},{"Name":"Name 3","Id":2,"Category":"category 3"}]
  </code></pre>
  
  <p>Hope this helps someone some day!</p>