0

For post method in android I have to send array of objects. I am retrieving elements from an arrayList and putting that retrieved element in JSONObject and than putting that in JSONobject in JSONArray.

JSONArray ccArray = new JSONArray();
{
 JSONObject object = new JSONObject();
 if (ccArrayList.size() != 0) {
 for (int i = 0; i < ccArrayList.size(); i++) {
 object.put("emailId", ccArrayList.get(i));
 ccArray.put(object);
 }
 } else {
 object.put("", "");
 }
}

When there are more than 2 or more than 2 elements in arraylist it is adding the last element in ccrray as many times as their are elements in the list.

output : "cc":[{"emailId":"[email protected]"},{"emailId":"[email protected]"}]

Phantômaxx
38.1k21 gold badges88 silver badges122 bronze badges
asked Nov 7, 2018 at 8:22
1
  • you need to put your JSONArray into a JSONObject. Commented Nov 7, 2018 at 8:28

3 Answers 3

1

Change code like this

 JSONObject obj = new JSONObject();
 JSONArray ccArray = new JSONArray();
 for (int i = 0; i < ccArrayList.size(); i++) {
 JSONObject object = new JSONObject();
 if (ccArrayList.size() != 0) {
 object.put("emailId", ccArrayList.get(i));
 ccArray.put(object);
 } else {
 object.put("", "");
 }
 }
 obj.put("cc",ccArray);
answered Nov 7, 2018 at 8:36
Sign up to request clarification or add additional context in comments.

Comments

1

Code:-

ArrayList<String> ccArrayList = new ArrayList<String>();
ccArrayList.add("[email protected]");
ccArrayList.add("[email protected]");
ccArrayList.add("[email protected]");
ccArrayList.add("[email protected]");
ccArrayList.add("[email protected]");
JSONArray ccArray = new JSONArray();
if(ccArrayList.size()>0){
 for(int i=0;i<ccArrayList.size();i++){
 JSONObject object = new JSONObject();
 try {
 object.put("emailId", ccArrayList.get(i));
 } catch (JSONException e) {
 e.printStackTrace();
 }
 ccArray.put(object);
 }
}

Output:-

ccArray: [{"emailId":"[email protected]"},{"emailId":"[email protected]"},{"emailId":"[email protected]"},{"emailId":"[email protected]"},{"emailId":"[email protected]"}]
answered Nov 7, 2018 at 8:42

Comments

0

That's because you declared JSONObject object = new JSONObject(); outside the for loop, so each time it loops the object adds a new JSONObject and also does the array. Try keeping it within the loop:

JSONArray ccArray = new JSONArray();
for (int i = 0; i < ccArrayList.size(); i++) {
 JSONObject object = new JSONObject();
 object.put("emailId", ccArrayList.get(i));
 ccArray.put(object);
}
answered Nov 7, 2018 at 8:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.