0

First, I want to add all ArrayList Values into JsonObject then I want to add these JSON object to JsonArray I have done wright in the first I have added all ArrayList to JsonObject but I can't add all JsonObject to JsonArray.

This is my code to convert json object to json Array:-

 submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONArray jsonArray = new JSONArray();

          JSONObject jsonObject = new JSONObject();

            for (int i = 0; i < records.size(); i++) {
                try {
                    jsonObject.put("sid", records.get(i).getsid());
                    jsonObject.put("name", records.get(i).getpName());


                    jsonArray.put(jsonObject));





                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Log.e("ATTENDANCE", jsonArray.toString() + "");
        }

    });

I am getting only the last value of the ArrayList please help me I want The output to be like these

  [
        {
              "student_no":"3924/08",
              "firstname":"olana
        },
        {
              "student_no":"4011/08",
              "firstname":"yallem"
        }
  ]

2 Answers 2

7

That is because you're creating only one object and putting it inside the for loop and you have to create new objects for every iteration.

Put your

JSONObject jsonObject = new JSONObject();

inside your for loop

like this

        for (int i = 0; i < records.size(); i++) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("sid", records.get(i).getsid());
                jsonObject.put("name", records.get(i).getpName());
                jsonArray.put(jsonObject));

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

very helpful it works like charm...thank you for your fast response
@OlanaKenea Glad i could help. All the best.
0
submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < records.size(); i++) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("sid", records.get(i).getsid());
                    jsonObject.put("name", records.get(i).getpName());
                    jsonArray.put(jsonObject));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Log.e("ATTENDANCE", jsonArray.toString() + "");
        }

    });

1 Comment

whats the difference between your answer and the answer below?

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.