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());
}
}
listingsas Steve, I can't help but think you are overwriting yourlistingssomewhere. Just speculation though -- you need to post what you did as @JonSkeet said.