Monday 26 August 2013

Create an array of JSON Objects

Create an array of JSON Objects

I need to create a JSON Object for an Arraylist. Below is the code
public boolean submitOrder(ArrayList<OrderDetailsData> orderList) {
serUri = "lists.json";
method = "post";
JSONObject jsonObject = new JSONObject();
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
try {
for (int i = 0; i < 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;
}
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.

No comments:

Post a Comment