java - how to create multiple JSON Object dynamically? -
i trying insert data multiple json objects don't know how create them dynamically in android.
in hard coded way like:-
jsonarray pdoinformation = new jsonarray(); jsonobject pdetail1 = new jsonobject(); jsonobject pdetail2 = new jsonobject(); jsonobject pdetail3 = new jsonobject(); pdetail1.put("productid", 1); pdetail1.put("qty", 3); pdetail1.put("listprice", 9500); pdetail2.put("productid", 2); pdetail2.put("qty", 4); pdetail2.put("listprice", 8500); pdetail3.put("productid", 3); pdetail3.put("qty", 2); pdetail3.put("listprice", 1500); pdoinformation.put(pdetail1); pdoinformation.put(pdetail2); pdoinformation.put(pdetail3);
but want create these jsonobject
dynamically don't know how many of them going needed while coding , in dynamically created jsonobject
data filled 3 arraylist
of productid
, qty
, listprice
.
so obvious number of dynamically created jsonobject
depend on size
of 1 arraylist
.
arraylist
:-
arraylist<string> productid = new arraylist<string>(); arraylist<string> qty = new arraylist<string>(); arraylist<string> listprice= new arraylist<string>();
list<jsonobject> myjsonobjects = new arraylist<jsonobject> (productid.size()); for(int i=0; i<productid.size(); i++) { jsonobject obj = new jsonobject(); obj.put("productid", productid.get(i) ); obj.put("qty", qty.get(i)); obj.put("listprice", listprice.get(i)); myjsonobjects.add(obj); }
at end jsonobjects in myjsonobjects.
Comments
Post a Comment