I have a for loop in which I add some attributes to the object and at the end I need to have a string (to pass it as a query parameter)
I need
{"items":[{"itemsCode":"item1","isAvailable":false}],"isActive":false},{"items":[{"entryCode":"item2","isSelected":true}],"isActive":false}
When I try
List<List> itemsList= new ArrayList<>();
itemsList.add(new Items(items, isBoolean));
ObjectMapper mapper = new ObjectMapper();
String itemsJson = mapper.writeValueAsString(itemsList);
POJO
public class Items {
private List<Item> items;
private Boolean isBoolean;
I get this:
[{"items":[{"itemsCode":"item1","isAvailable":false}],"isActive":false},{"items":[{"entryCode":"item2","isSelected":true}],"isActive":false}]
please help how to get rid of an array brackets, but keeping the convention of adding those object to a list in a for loop
List<List> itemsList= new ArrayList<>();
) if you don't want a list? Or vice versa, why don't you want a list, if you need a list?itemsJson = itemsJson.substring(1, itemsJson.length() - 1);
.Item
to String separately, then joining those with commas. And then figure out how you're ever going to parse that string back into objects.ObjectMapper
(I assume this is the Jackson class) may not be a good solution.