Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:20:43+00:00 2026-05-16T08:20:43+00:00

I am trying to add an object to an arraylist but when I view

  • 0

I am trying to add an object to an arraylist but when I view the results of the array list, it keeps adding the same object over and over to the arraylist. I was wondering what the correct way to implement this would be.

    public static ArrayList<Person> parsePeople(String responseData) {
    ArrayList<Person> People = new ArrayList<Person>();
    try {

        JSONArray jsonPeople = new JSONArray(responseData);
        if (!jsonPeople.isNull(0)) {
            for (int i = 0; i < jsonPeople.length(); i++) {
                People.add(new Person(jsonPeople.getJSONObject(i)));
            }

        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {

    }

    return People;
}

I have double checked my JSONArray data and made sure they are not duplicates. It seems to keep adding the first object over and over.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:20:44+00:00Added an answer on May 16, 2026 at 8:20 am

    There is either a problem with the responseData string or the constructor.

    If the class receives a response Data object that looks like the following

    String responseData = 
    "[{ 
    \"first_name\" : \"fred\" , 
    \"last_name\" : \"Nobody\"  
    }, 
    { 
    \"first_name\" : \"John\" , 
    \"last_name\" : \"Somebody\"  
    }]";
    

    Then your Contact class should look like

    public class Contact {
        String fname;
        String lname;
    
        public Contact(JSONObject obj){
            System.out.println(obj);
            try {
                fname = (String)obj.get("first_name");
                lname = (String)obj.get("last_name");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    //get and set methods
    }
    

    There should be no reason based on your logic to have the same record show up twice. Make sure your JSON string has the correct format coming in. I would suggest adding more System.out or Log4j calls in the application to determine every step. Worst case step through the application with a debug session.

    PS – I built you app by adding the above code and it worked fine. So you have the grasp on adding the elements to the ArrayList properly. Could you also show how you print the array back out? Maybe the issue is there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add an object to a list but since I'm adding
Trying to add a class object into a List using reflection, but when invoking
I'm trying to add two object that they are in the same class. In
I'm trying to add results from an object to newly created HTML elements: *long
I am trying to add time to a Date object in javascript but am
I'm trying to add a region using Azure Datacache object but I'm getting such
I'm trying to add new object to my ArrayList if it satisfy the condition.
Hi I am trying to add an object into an Arraylist, I am using
I am trying to add the datetime object of a person. Whenever the birth
I'm trying to add a unittest attribute to an object in Python class Boy:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.