0

I have this structure in Firebase

    "shared_items" : {
    "-KgGHdgE3L_m6ppVgn99" : {
      "_id" : 14,
      "added_date" : "08/Mar/2017",
      "shared_with_emails" : "{\"abc@abc*com\":{\"name\":\"Customer Care\"},\"xyz@xyz*com\":{\"name\":\"Customercare\"}}",
      "user_display_name" : "Logged in user",
      "users_email" : "loggedinUser@gmail.com"
    }
  }

My questions:

  1. When I am saving JSON data in "shared_with_emails" key then my data is automatically appended with "\" slash. Is this normal or I am doing something wrong here?
  2. How can I get entire node based on email Ids present in this JSON object.

Function to create JSON objects from provided contacts..

public class JsonUtils {
    final private static String TAG = JsonUtils.class.getSimpleName();

    public String ContactsToJson() {
        ArrayList<ContactsModel> listOfContacts = new ArrayList<>();
        listOfContacts.add(new ContactsModel("abc@gmail.com", "abc"));
        listOfContacts.add(new ContactsModel("xyz@gmail.com", "xyz"));
        listOfContacts.add(new ContactsModel("mnop@yahoo.com", "mnop"));


        JSONObject jsonObjectChild;
        JSONObject jsonObjectRoot = new JSONObject();

        for (int i = 0; i < listOfContacts.size(); i++) {
            ContactsModel model = (ContactsModel) listOfContacts.get(i);
            try {
                jsonObjectChild = new JSONObject();
                jsonObjectChild.put("name", model.getContactName());
                jsonObjectRoot.put(model.getContactMail(), jsonObjectChild);
            } catch (JSONException e){
                e.printStackTrace();
            }

        }

        System.out.println(jsonObjectRoot.toString());
        return jsonObjectRoot.toString();
    }

    public class ContactsModel {

        private int id;
        private String mContactName;
        private String mContactMail;


        public ContactsModel(String contactMail, String contactName) {
            this.mContactName = contactName;
            this.mContactMail = contactMail;
        }

        public String getContactName() {
            return mContactName;
        }

        public String getContactMail() {
            return mContactMail;
        }
    }

}

Json data on Firebase

enter image description here

Deepesh
  • 523
  • 4
  • 11
  • If you're sharing a "thing" with multiple users, it turns into a categorization problem. For one approach to model that, see http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Feb 19 '17 at 21:48
  • @Frank I have followed your post and tried the steps you showed us but still not able to achieve the desired functionality. I have updated the question request you to kindly take a look again. – Deepesh Mar 28 '17 at 11:34
  • 1. You're not storing JSON, but storing a string. Without seeing the code that produces this, it's hard to say anything about the cause/fix. 2. Nothing in this structure is changed to model the data as I described in the answer I linked. – Frank van Puffelen Mar 28 '17 at 13:37
  • @FrankvanPuffelen updated the code with function. Have a look please. I think I am returning JsonObj.toString() instead of object. Please let me know where I am messing the things up. – Deepesh Mar 28 '17 at 15:30
  • @FrankvanPuffelen I tried to save JsonObject on Firebase and have attached a new screenshot (appended with post) but still unable to get the snapshot by using ref.child(). Is data in screenshot looks fine ? – Deepesh Mar 29 '17 at 12:16
  • @FrankvanPuffelen please help me to get through with this problem.. I am still waiting for your response. – Deepesh Mar 30 '17 at 10:11

1 Answers1

0

You can use ArrayList<String> to have multiple emails saved under shared_with_email. When you retrieve your data, save it inside an object and search whether the object contains the email.

ArrayList<String> keys = new ArrayList<>;

rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(Datasnapshot snap:datasnapshot.getChildren()){
                ArrayList<yourObject> current = snap.getValue();
                if (current.shared_with_email.contains(someEmail)){
                    keys.add = snap.getKey(); //Check this, all we need is the key of our object. kd47qjB.... in this case
                } 
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Once you have all the keys. Add listeners.

for(String key: keys){
    rootRef.child(key).addValueEventListener(new ValueEventListener(){
        @override
        public void onDataChange(Datasnapshot datasnapshot){
            //You successfully added listener to the key where user has its email. 
            //Do your work here.
        }
    });
}

You will have to create a class yourObject which resembles the data in your firebase database.

Srujan Barai
  • 2,295
  • 4
  • 29
  • 52
  • I would only like to receive snapshot data whenever the currently logged in user's mail id is present in this list. – Deepesh Mar 16 '17 at 09:06
  • You'll have to do a little bit of work for that. Where I have written "Attach your listener here", you have to do something like, if email is present, save the key of that node in some ArrayList. After you're done with all objects, attach your listeners individually. – Srujan Barai Mar 16 '17 at 12:32
  • Can you please elaborate your last comment with the help of any example. URL of any web link would work too. Thanks for your help. – Deepesh Mar 17 '17 at 03:40
  • I haven't tested this code so better make necessary changes. – Srujan Barai Mar 17 '17 at 07:06