I have problem with displaying data from an array of classes. The data is coming from JSON:
[{"id":1,"userId":1,"date":"2020-03-02T00:00:02.000Z","products":[{"productId":1,"quantity":4},{"productId":2,"quantity":1},{"productId":3,"quantity":6}],"__v":0},{"id":2,"userId":1,"date":"2020-01-02T00:00:02.000Z","products":[{"productId":2,"quantity":4},{"productId":1,"quantity":10},{"productId":5,"quantity":2}],"__v":0},{"id":3,"userId":2,"date":"2020-03-01T00:00:02.000Z","products":[{"productId":1,"quantity":2},{"productId":9,"quantity":1}],"__v":0},{"id":4,"userId":3,"date":"2020-01-01T00:00:02.000Z","products":[{"productId":1,"quantity":4}],"__v":0},{"id":5,"userId":3,"date":"2020-03-01T00:00:02.000Z","products":[{"productId":7,"quantity":1},{"productId":8,"quantity":1}],"__v":0},{"id":6,"userId":4,"date":"2020-03-01T00:00:02.000Z","products":[{"productId":10,"quantity":2},{"productId":12,"quantity":3}],"__v":0},{"id":6,"userId":8,"date":"2020-03-01T00:00:02.000Z","products":[{"productId":18,"quantity":1}],"__v":0}]
Screen how it's currently works: test
The array length is ok but i dont know how i can call the productId and quantity for these array correctly, any suggestions?
Cart class:
public class Cart {
int id;
int userId;
Date date;
ProductsCart[] products;
String __v;
//getters setters here
}
ProductCart class
public class ProductsCart {
int productId;
int quantity;
//constructors and getters setters here
}
That's part of my main code:
List<Cart> carts = mapper.readValue(data3, typeReference2);
for (Cart c : carts) {
System.out.println("\nId: " + c.getId() +
"\nUser Id: " + c.getUserId() + "\nDate: " +
c.getDate() + "\nProducts: " + c.getProducts()); <-- here is the problem
}