我在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"
}
}
我的问题:
"shared_with_emails"
密钥中保存JSON数据时,我的数据会自动附加"\"
斜杠。这是正常的还是我在这里做错了什么?从提供的联系人创建JSON对象的函数..
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;
}
}
}
Firebase上的Json数据
答案 0 :(得分:0)
您可以使用ArrayList<String>
在shared_with_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) {
}
});
获得所有钥匙后。添加监听器。
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.
}
});
}
您必须创建类似于firebase数据库中的数据的类yourObject
。