简体   繁体   中英

How to parse Object of Map<String, Object> returned from documentSnapshot.getData() of Google Cloud Firestore into a POJO?

documentSnapshot.getData() method of Cloud Firestore returns a Map< String, Object >. My db structure looks like below:

数据库结构/模式

Now I have a POJO as below:

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

As you see, all I want is a list of Object from the Map< String, Object > returned from documentSnapshot.getData() method. The String, ie, the key part, has to be ignored. I don't want to use keys to extract from Map as I want the algorithm to dynamically create a list of all plans (POJO) irrespective of the number of key-value pairs in the map.

Below is the code I'm using:

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

I tried many ways but I am unable to parse that Object into my POJO. Please help. Thanks in advance.

The best way is, you should use the same POJO class that you were using to save the data. As it is a complex class and has object of Plan class so if you use that one it will be better. Let your original class name is Familiy_pack.

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


                    }
                }
            }
        });

Now, use can easily get the values from Plan object.

Update: Refer Convert DocumentSnapshot Data to List as you want list of object

Official documentations says

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

so try

Plan plan = documentSnapshots.toObject(Plan.class);

Ref - https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot

try QuerySnapshot instead of 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);
                }
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM