【问题标题】:How to read data from nested array in Firestore如何从 Firestore 中的嵌套数组中读取数据
【发布时间】:2022-01-20 11:54:38
【问题描述】:

我的 Firestore 中有以下结构,我想读取数据并将其存储在 ArrayList 中,就像我有“amountArrayList”,它将从 Firestore 中的“事务”字段读取数据,我想读取所有“金额”来自“事务”字段的字段并制作它的数组列表,以便我可以以列表方式显示它。

我的代码

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());
    }
}

输出

[{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=}]

【问题讨论】:

标签: java android firebase google-cloud-platform google-cloud-firestore


【解决方案1】:

由于transactions 是一个数组字段,您从entry.getValue() 获得的值是对象的List。由于 JSON 中的这些对象中的每一个都有属性,因此它们在 Java 代码中再次成为 Map&lt;String, Object&gt;

打印金额的简单方法如下:

List transactions = document.get("transactions");
for (Object transaction: transactions) {
  Map values = (Map)transaction;
  System.out.println(values.get("amount")
}

【讨论】:

    【解决方案2】:

    虽然 Frank van Puffelen 的回答可以很好地工作,但有一个解决方案,您可以直接将“事务”数组映射到自定义对象列表中。假设您有一个如下所示的类声明:

    class User {
        public String balance, email, firstname, lastname, password, username;
        public List<Transaction> transactions;
    
        public User(String balance, String email, String firstname, String lastname, String password, String username, List<Transaction> transactions) {
            this.balance = balance;
            this.email = email;
            this.firstname = firstname;
            this.lastname = lastname;
            this.password = password;
            this.username = username;
            this.transactions = transactions;
        }
    }
    

    一个看起来像这样的:

    class Transaction {
        public String amount, dateToStr, description, timeToStr, transactionType;
    
        public Transaction(String amount, String dateToStr, String description, String timeToStr, String transactionType) {
            this.amount = amount;
            this.dateToStr = dateToStr;
            this.description = description;
            this.timeToStr = timeToStr;
            this.transactionType = transactionType;
        }
    }
    

    要获取列表,很简单:

    docRef.get().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                List<Transaction> transactions = document.toObject(User.class).transactions;
                List<String> amountArrayList = new ArrayList<>();
                for(Transaction transaction : transactions) {
                    String amount = transaction.amount;
                    amountArrayList.add(amount);
                }
                // Do what you need to do with your amountArrayList
            }
        }
    });
    

    您可以在以下文章中阅读更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多