android - Create an array of JSON Objects -
i need create json object arraylist. below 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 { (int = 0; < 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 creating object details of last row item @ each position. if orderlist has 3 rows, jsonobject created has 3 rows 3rd row data in 3. seems overriding data rows latest row fetched. tried couple of other ways still not getting desired result. please advise. thanks.
json object created:
{"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"}]}
the above object has last item in each row.
small mistake
try { (int = 0; < orderlist.size(); i++) { jsonobject json = new jsonobject(); // update here 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; }
Comments
Post a Comment