title: Create an array of JSON Objects
body: <p>I need to create a JSON Object for an Arraylist. Below is the code</p>
  
  <pre><code>public boolean submitOrder(ArrayList&lt;OrderDetailsData&gt; orderList) {
  
          serUri = "lists.json";
          method = "post";
  
  
          JSONObject jsonObject = new JSONObject();
  
          JSONObject json = new JSONObject();
  
          JSONArray array = new JSONArray();
          try {
              for (int i = 0; i &lt; orderList.size(); i++) {
                  json.put("orderno", orderList.get(i).getOrderNumber()
                          .toString());
                  json.put("tableno", orderList.get(i).getTableNumber()
                          .toString());
                  json.put("itemname", orderList.get(i).getItemName().toString());
                  json.put("amount", orderList.get(i).getAmount().toString());
  
                  json.put("ordstatus", orderList.get(i).getOrderStatus()
                          .toString());
                  array.put(json);
  
  
  
              }
  
           catch (JSONException je) {
              return false;
          }
  
          try {
  
              jsonObject.put("list", array);
          } catch (JSONException je) {
              return false;
          }
  
          WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask();
          webServiceTask.execute(serUri, method,jsonObject, this);
          return true;
      }
  </code></pre>
  
  <p>The problem is its creating object with details of last row item at each position. Say if my orderList has 3 rows, the jsonObject created has 3 rows but with 3rd row data in all 3. Seems like its overriding data for all the rows with latest row fetched. I tried with couple of other ways but still not getting desired result. Please advise. Thanks.</p>
  
  <p>JSON Object created:</p>
  
  <pre><code>{"list":[{"amount":"10.50","orderno":"0220130826163623","quantity":"1","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"}]}
  </code></pre>
  
  <p>The above object has only last item in each row.</p>