1

Okay, so lets say that the following is my JSON file. I can get the ID and name from the file just fine, but with the listings, it appears that it is only reading the finally listings into each person. So John and Dave both have the listings that Steve has. Do you know where I'm going wrong with this?

[
    {
        "id":1,
        "name":"John",
        "listings":[
            {
                "id":1,
                "other_id":34,
            },
            {
                "id":2,
                "other_id":16,
            },
            {
                "id":3,
                "other_id":39,
            }
        ]
    },
    {
        "id":2,
        "name":"Dave",
        "listings":[
            {
                "id":1,
                "other_id":156,
            },
            {
                "id":2,
                "other_id":189,
            },
            {
                "id":3,
                "other_id":312,
            }
        ]
    },
    {
        "id":3,
        "name":"Steve",
        "listings":[
            {
                "id":1,
                "other_id":876,
            },
            {
                "id":2,
                "other_id":534,
            },
            {
                "id":3,
                "other_id":456,
            }
        ]
    }
]

And my java code

ArrayList<Person> people = new ArrayList<>();

JSONArray array = (JSONArray) parser.parse(reader);

for (Object object : array){
    JSONObject jsonObject = (JSONObject) object;
    int id = (int) jsonObject.get("id");
    String name = (String) jsonObject.get("name");

    ArrayList<Listing> listing = new ArrayList<>();

    JSONArray listings = (JSONArray) jsonObject.get("listings");

    for(Object item : listings){
        JSONObject jsonItem = (JSONObject) item;
        int itemID = (int) jsonItem.get("id");
        int otherID = (int) jsonItem.get("other_id");

        Listing temp = new Listing(itemID, otherID);

        listing.add(temp);

    }

    people.add(new Person(id, name, listing));

}

for (Person person : people) {
    System.out.println("ID: " + person.getId() + ", " + " Name: " + person.getName());
    for (Listing list : person.getListing()) {
        System.out.println("ID: " + list.getID() + ", " + " OtherID: " + list.getOtherID());
    }
}
5
  • "other_id"876 - you miss colon Commented Mar 16, 2015 at 7:22
  • 1
    You haven't shown what you're doing with the results. Please provide a short but complete program which demonstrates the issue. Commented Mar 16, 2015 at 7:23
  • When you say John and Dave has the same listings as Steve, I can't help but think you are overwriting your listings somewhere. Just speculation though -- you need to post what you did as @JonSkeet said. Commented Mar 16, 2015 at 7:25
  • listing.add(id, otherID) is an ArrayList.add(int index,E element), happily you indexes are 1,2,3. So otherID getting replaced. Commented Mar 16, 2015 at 7:41
  • 1
    @aug Yeah, had one too many copy&paste and was overwriting. You have no idea how long i've been looking at this, how dumb I feel right now.. Commented Mar 16, 2015 at 8:45

2 Answers 2

1

more easy to use jackson parser.

ObjectMapper mapper;
mapper.readValue(jsonFile, new TypeReference<List<Person>>(){});

It will do all the work for you (populate the Person object)

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

Comments

0

The listing.add(id, otherID) you are using is an ArrayList.add(int index,E element) So I have changed a little bit check this working.

for (int index = 0; index < array.length(); index++) {
        JSONObject jsonObject = (JSONObject) array.get(index);
        int id = (int) jsonObject.get("id");
        String name = (String) jsonObject.get("name");
        List<Listing> listing = new ArrayList<>();
        JSONArray listings = (JSONArray) jsonObject.get("listings");
        for (int index1 = 0; index1 < listings.length(); index1++) {
            JSONObject jsonItem = (JSONObject) listings.get(index1);
            int itemID = (int) jsonItem.get("id");
            int otherID = (int) jsonItem.get("other_id");
            Listing listing2 = new Listing(itemID, otherID);
            listing.add(listing2);
            System.out.println(name + " " + itemID + " " + otherID);
        }
        people.add(new Person(id, name, listing));
    }

Comments

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.