2

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>();
9
  • you want send data without names ?without keys? Commented Aug 18, 2014 at 7:53
  • @DIVA with name and key Commented Aug 18, 2014 at 7:54
  • post your arraylist to get complete code Commented Aug 18, 2014 at 7:54
  • size of all of them are equal , yes? Commented Aug 18, 2014 at 8:01
  • @mmlooloo yes all are same Commented Aug 18, 2014 at 8:01

4 Answers 4

10
 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.

Sign up to request clarification or add additional context in comments.

3 Comments

will it store the data like:- {{productid:1, qty:3, listprice:9500}, {productid:2,qty:2, listprice:8500}} or {productid:1, qty:3, listprice:9500, productid:2, qty:2, listprice:8500}`???
this store json object , not json array , for example it stores {productid:1, qty:3, listprice:9500} at myJSONObjects.get(0); and {productid:2,qty:2, listprice:8500} at myJSONObjects.get(1); and so ...
Okay, I will let you know after testing it. Thanks :)
3

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.

2 Comments

but how will I put the data into it as I don't know its name?
Sir, its almost same except there is no use of ArrayList in your code and at that time you didn't responded to my query whereas others were responding. I accepted that answer not only because it worked exactly as I wanted but also because if someone else will come searching for same question then he don't have to do much modifications in code. No disrespect, I even up-voted your answer.
1

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.

2 Comments

will this add the product details of each product in different JSON objects?
yes..and it is a good way to keep data together.It will ensure you that correct quantity and price is assigned to its productid.
0

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.

1 Comment

obviously there must be a coma betwen each interaction. Just a quick example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.