Amit Anand
Amit Anand

Reputation: 1225

how to create multiple JSON Object dynamically?

I am trying to insert data into multiple JSON objects but I don't know how to create them dynamically in android.

In the hard coded way it is something 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 I want to create these JSONObject dynamically as I don't know how many of them are going to be needed while coding and in those dynamically created JSONObject the data will be filled from three ArrayList of productid, qty and listprice.

So its obvious that the number of those dynamically created JSONObject will depend on the size of any one ArrayList.

ArrayList :-

ArrayList<String> productid = new ArrayList<String>();
ArrayList<String> qty = new ArrayList<String>();
ArrayList<String> listprice= new ArrayList<String>();

Upvotes: 2

Views: 15775

Answers (4)

mmlooloo
mmlooloo

Reputation: 18977

 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 the end all JSONObjects are in myJSONObjects.

Upvotes: 10

jordi
jordi

Reputation: 1187

JSON is just a text String. You could simply write something like:

    String jsonString = "[";
    jsonString = jsonString  + "{\""productid\":1",\"qty\":3,\"listprice\":9500}";
    jsonString = jsonString  + "{\""productid\":2",\"qty\":4,\"listprice\":8500}";
    jsonString = jsonString  + "{\""productid\":3",\"qty\":2,\"listprice\":1500}";
...
    jsonString = jsonString  + "]";

This could be the raw way of doing it.

I am using (not in android but in jsp, so could not be posible) gson and it also works with arraylist object, so you can create the array of objects and after that just ask him to generate the json text.

Upvotes: 0

Ninad Pingale
Ninad Pingale

Reputation: 7079

In this design, how will you relate data with each other if it is divided in multiple Arraylists.

It seems you need to redesign your data structure a bit.

Instead of using three ArrayLists you should keep one Arraylist.

That Arraylist will hold object of beans.

For ex.

class product{
private double productid;
private double listprice;
private long qty;

// getters and setters
}

and keep all objects in one Arraylist and then by looping through it you get all three values together while creating JSON.

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128458

I want to create these JSONObject dynamically as I don't know how many of them are going to be needed while coding.

As you are already having ArrayList, iterate through it and create a new JSONObject in each iteration and put it inside ArrayList<JSONObject>.

For example: JSONObject objJSON;

for(int i=0; i<numberOfItems; i++) {
    objJSON = new JSONObject();
    objJSON.put("productid", 1);
    objJSON.put("qty", 3);
    objJSON.put("listprice", 9500);

    pdoInformation.put(objJSON);
}

The data will be filled from three ArrayList of productid, qty and listprice

You shouldn't take different ArrayLists because you have to manage each lists as many as you have, instead of that create a single ArrayList of type user defined class. For example, ArrayList<Product> where Product type would contain setter/getter methods.

Upvotes: 3

Related Questions