I have the below structure in my Firestore and I want to read data and store it in ArrayList like I have “amountArrayList” which will read data from the “transactions” field in Firestore, I want to read all the “amount” fields from “transactions” field and make array list of it so that I can show it in list manner.
My code
Map<String, Object> map = document.getData(); for (Map.Entry<String, Object> entry : map.entrySet()) { if (entry.getKey().equals("transactions")) { System.out.println(entry.getValue().toString()); } }
Output
[{transactionType=Credit, amount=3000, dateToStr=17/12/2021, timeToStr=08:06:10, description=}, {transactionType=Credit, amount=2000, dateToStr=17/12/2021, timeToStr=08:06:50, description=}]
Advertisement
Answer
Since transactions
is an array field, the value you get from entry.getValue()
is a List
of objects. Since each of these objects in the JSON has properties, they each will be a Map<String, Object>
again in the Java code.
A simple way to print the amounts would be something like:
List transactions = document.get("transactions"); for (Object transaction: transactions) { Map values = (Map)transaction; System.out.println(values.get("amount") }