I have to create JSON response like the below one.
[{"Name":"Shampoo","Qty":"3","Amt":"300"},
{"Name":"Soap","Qty":"1","Amt":"50"}]
Code:
ArrayList<String> al= new ArrayList<String>();
al.addAll(name);
al.addAll(qty);
al.addAll(price);
Gson gson = new Gson();
String json = gson.toJson( al );
System.out.println(json);
JSONObject pa = new JSONObject();
childData = new JSONObject();
try {
childData.put("Name", name);
childData.put("Qty", qty);
childData.put("Amt", price);
pa.put("Detais",childData);
} catch (JSONException e) {
e.printStackTrace();
}
But the above code creates the response like this
{"Name":"[Shampoo, Soap]","Qty":"[3, 1]","Amt":"[300, 50]"}