I have a class file
public class Name {
private String id;
public String getId() {
return id;
}
}
This is the test file
CollectionType javaType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Name.class);
ArrayList<Names> nameList = mapper.readValue(json, javaType);
for (Names nameValue : nameList) {
List<String> id = new ArrayList<>();
id.add(nameValue.getId());
System.out.println(id);
json
[{"id":"1"},{"id":"2"}]
In the JSON output there is more than one id, and I am trying to store them in one list but it only adds the first one ("1"), how do I store both in one list. Please help
forloop? Your identation doesn't help too much to figure it out. In every iteration you're creating a newList, take theidoutside theforloop