Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解析Map的对象<;字符串,对象>;从Google Cloud Firestore的documentSnapshot.getData()返回到POJO?_Java_Android_Dictionary_Google Cloud Firestore_Pojo - Fatal编程技术网

Java 如何解析Map的对象<;字符串,对象>;从Google Cloud Firestore的documentSnapshot.getData()返回到POJO?

Java 如何解析Map的对象<;字符串,对象>;从Google Cloud Firestore的documentSnapshot.getData()返回到POJO?,java,android,dictionary,google-cloud-firestore,pojo,Java,Android,Dictionary,Google Cloud Firestore,Pojo,Cloud Firestore的documentSnapshot.getData()方法返回一个映射。我的数据库结构如下所示: 现在我有一个POJO,如下所示: public class Plan { private ArrayList<String> items; private String name; private int price; private int years; public Plan() { } p

Cloud Firestore的documentSnapshot.getData()方法返回一个映射。我的数据库结构如下所示:

现在我有一个POJO,如下所示:

public class Plan {

    private ArrayList<String> items;
    private String name;
    private int price;
    private int years;

    public Plan() {
    }

    public Plan(ArrayList<String> items, String name, int price, int years) {
        this.items = items;
        this.name = name;
        this.price = price;
        this.years = years;
    }

    public List<String> getItems() {
        return items;
    }

    public void setItems(ArrayList<String> items) {
        this.items = items;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getYears() {
        return years;
    }

    public void setYears(int years) {
        this.years = years;
    }
}
公共课程计划{
私有ArrayList项;
私有字符串名称;
私人int价格;
私人年资;
公共计划(){
}
公共计划(ArrayList项目、字符串名称、整数价格、整数年){
这个项目=项目;
this.name=名称;
这个价格=价格;
本年=年;
}
公共列表getItems(){
退货项目;
}
公共无效集合项(ArrayList项){
这个项目=项目;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public int getPrice(){
退货价格;
}
公共无效设置价格(整数价格){
这个价格=价格;
}
公共年数(){
回归年;
}
公共年数(整数年){
本年=年;
}
}
如您所见,我所需要的只是从documentSnapshot.getData()方法返回的Map中的对象列表。必须忽略字符串,即关键部分。我不想使用键从映射中提取,因为我希望算法能够动态创建所有计划的列表(POJO),而不考虑映射中键-值对的数量

下面是我正在使用的代码:

documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                    Map<String, Object> planMap = documentSnapshot.getData();
                    // how to parse the Object inside Map<String, Object> above to POJO Plan.class here
                    }
                }
            }
        });
documentReference.get().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(文档快照文档快照){
if(documentSnapshot.exists()){
Map planMap=documentSnapshot.getData();
//如何解析上面映射到POJO Plan.class的对象
}
}
}
});
我尝试了很多方法,但我无法将该对象解析到我的POJO中。请帮忙。提前感谢。

更新:根据需要参考对象列表

官方文件说

 <T> T toObject(Class<T> valueType) - 
 Returns the contents of the document converted to a POJO or null if the document doesn't exist.

Ref-

尝试
QuerySnapshot
而不是
DocumentSnapshot

documentReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshot) {
                if (documentSnapshots.isEmpty()) {
                    Log.d(TAG, "onSuccess: LIST EMPTY");
                    return;
                } else {
                    // Convert the whole Query Snapshot to a list
                    // of objects directly! No need to fetch each
                    // document.
                    List<Plan> plans = documentSnapshot.toObjects(Plan.class);   

                    // Add all to your list
                    mArrayList.addAll(types);
                    Log.d(TAG, "onSuccess: " + mArrayList);
                }
            }
        });
documentReference.get().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(QuerySnapshot文档快照){
if(documentSnapshots.isEmpty()){
Log.d(标记“onSuccess:LIST EMPTY”);
回来
}否则{
//将整个查询快照转换为列表
//直接获取对象!无需获取每个对象
//文件。
列表计划=documentSnapshot.ToObject(Plan.class);
//将所有内容添加到您的列表中
mArrayList.addAll(类型);
Log.d(标记“onSuccess:+mArrayList”);
}
}
});

最好的方法是,使用与保存数据相同的POJO类。因为它是一个复杂的类,并且有Plan类的对象,所以如果您使用那个类,它会更好。让您的原始类名为Family_pack

documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                   Family_pack f = documentSnapshots.toObject(Family_pack .class);


                    }
                }
            }
        });
documentReference.get().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(文档快照文档快照){
if(documentSnapshot.exists()){
Family\u pack f=documentSnapshots.toObject(Family\u pack.class);
}
}
}
});

现在,使用可以很容易地从Plan对象获取值。

尝试
List plans=documentSnapshots.toObjects(Plan.class)无法解析对象的方法扫描您是否添加了正在使用的代码?@Niraj addedabove@AlexMamo我的问题没有解决办法。因此,我使用了不同的方法来解决这个问题。我将db模式从映射更改为数组,创建了一个具有arraylist属性的POJO,并将文档直接转换为该POJO。这样,它适用于数组(文档)中任意数量的值。请正确阅读我的问题。我不想将文档直接转换为POJO。我只希望文档中的值部分作为POJO列表。@Rahul我阅读了标题和注释“//如何解析上面映射到POJO Plan.class的对象”,并理解您需要POJO对象。更新我的ans以获取列表。也许你也想更新你的问题标题。更新后的答案仍然不能解决我的问题。请理解我的用例。请看一下数据库模式。我不希望在一个集合中有单独的文档。我需要在一个文档中包含多个类似文档的数据,因为它们总是一起获取,所以不必要的多文档获取会增加成本。你收到了吗?如果您需要更多infoaddOnSuccessListener(OnSuccessListener),请告诉我。即使这样也不行。如果您遵守我的db架构,“name”无法直接访问。它位于每个键值对的值部分内。还要注意,文档中可以有任意数量的键值对,并且算法应该适应它。请添加代码,如何在firestore中保存数据。如果使用任何POJO,也要添加@Rahul。此方法不会像我之前所说的那样工作,我希望用于文档中任意数量的键值对的逻辑。这样,我必须为每个键值对预先创建一个类,这不是一个好方法。
documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                   Family_pack f = documentSnapshots.toObject(Family_pack .class);


                    }
                }
            }
        });