I ve JSONObject named childData which contains name, quantity and price of each item and it is added in JSONArray pa. But after each iteration previous iteration output value of childData is getting replaced by the value of current iteration output value in pa.
Code:
JSONArray pa = new JSONArray();
JSONObject childData = new JSONObject();
for(int i=0; i<name.size();i++) {
childData.put("Name", name.get(i));
childData.put("Qty", qty.get(i));
childData.put("Amt", price.get(i));
pa.put(childData);
}
is producing the output like below
childData= {"Name":"Shirt","Qty":"1","Amt":"300"}
pa= [{"Name":"Shirt","Qty":"1","Amt":"300"}]
child= {"Name":"Coat","Qty":"1","Amt":"210"}
pa= [{"Name":"Coat","Qty":"1","Amt":"210"},{"Name":"Coat","Qty":"1","Amt":"210"}]