-2

I am trying to add content of a json file to an arraylist. I have done this a couple of times before but I cannot figure out what is wrong in this particular case that I cannot add anything to the arraylist.

So I have a :

private List<Person> persons = new ArrayList<Person>();

and this is how I load the json file from assets folder (from this answer):

public String loadJSONFromAsset() {
        String json = null;
        try {
            // json file name
            InputStream is = this.getAssets().open("file.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

and to write:

public void writeJson(){
    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray response = new JSONArray(loadJSONFromAsset());


        for (int i = 0; i < response.length(); i++) {
            Person person = new Person();
            JSONObject jo_inside = response.getJSONObject(i);

            Log.d(TAG, jo_inside.getString("firstName"));

            //Add values in `ArrayList`
            person.setName(obj.getString("firstName"));
            person.setAge(obj.getString("age"));
            person.setPhoto(obj.getInt("id"));
            // Add to the array
            persons.add(person);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

When I try to print the content of persons array for testing purposes, I get nothing using the above method. However, if I insert a person like this :

persons.add(new Person("John", "23 years old", 1)); 

then it will be added to the array.

I think there is a minor mistake somewhere but I can't find the solution.

Community
  • 1
  • 1
Rain Man
  • 1,163
  • 2
  • 16
  • 49
  • 1
    You've only got a single `Person` object - you're changing the properties of that `Person` on each iteration of the loop, but adding multiple references to the same object to the list. That may not be the only problem, but it's a significant one. – Jon Skeet Jul 23 '15 at 08:12
  • I'd also advise you to be more precise around terminology - you have a list, not an array. An array would be something like `Person[]`. (An `ArrayList` is a list which is backed by an array internally.) – Jon Skeet Jul 23 '15 at 08:13
  • put this line Person person = new Person(); with in the for loop – luckwithu Jul 23 '15 at 08:14
  • @luckwithu yes, I have tried that before and it didnt work – Rain Man Jul 23 '15 at 08:15
  • "It didn't work" provides us with almost zero information. What happened? What have you tried to diagnose this? Have you stepped through the code with a debugger? What happened? – Jon Skeet Jul 23 '15 at 08:16
  • (I would also point out that you shouldn't use `InputStream.available()` like this. You should pretty much always loop, reading until `read` returns -1.) – Jon Skeet Jul 23 '15 at 08:17
  • @JonSkeet, when I tried to print the content of the list, it printed nothing. Also nothing was printed on the screen when trying to pass the firstname – Rain Man Jul 23 '15 at 08:17
  • Not sure what you mean by "when trying to pass the firstname" - but you should step through in a debugger. – Jon Skeet Jul 23 '15 at 08:18
  • @JonSkeet I am using an adapter to pass each value to a textview – Rain Man Jul 23 '15 at 08:19
  • @JonSkeet in the logcat, it doesnt show any error – Rain Man Jul 23 '15 at 08:25
  • It's still not clear whether you've tried debugging... – Jon Skeet Jul 23 '15 at 08:32

1 Answers1

0

There are few corrections needed in your code. Check the comments in the code block.

    public void writeJson(){
    try {
        //this line is not needed, since you are reading array & not not json object 
        //JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray response = new JSONArray(loadJSONFromAsset());

        for (int i = 0; i < response.length(); i++) {
            JSONObject jo_inside = response.getJSONObject(i);

            Log.d(TAG, jo_inside.getString("firstName"));

            //Add values in `ArrayList`
            //for setting the values use object read from response array 
            Person person = new Person();
            person.setName(jo_inside.getString("firstName"));
            person.setAge(jo_inside.getString("age"));
            person.setPhoto(jo_inside.getInt("id"));
            // Add to the array
            persons.add(person);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } 
}

This shall solve your issue.

Ashwini Shahapurkar
  • 6,586
  • 6
  • 26
  • 35