Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate an array of JSON Objects
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload