我使用Firebase作为后端,我能够获取数据,但我无法将其映射到Java类。我也尝试使用JSONObject进行映射,但问题是我应该忽略密钥(1),因为我根据电子邮件值查询。
数据
users
1:
email: sample@email.com
firstName: Big
lastName: Ben
groups:
instructor: true
与Firebase相关的代码:
Firebase ref = new Firebase("firebaseURL");
Query queryRef = ref.orderByChild("email").equalTo(mEmailEdit.getText().toString()).limitToFirst(1);
// Attach an listener to read the data at our posts reference
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
System.out.println("user : " + user);
System.out.println("children : " + snapshot.getChildren().iterator().next());
mProgressDialog.dismiss();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
User.java
public class User {
private String email;
private String firstName;
private String lastName;
private Group group;
public User(){}
public User(String email, String firstName, String lastName) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return email.equals(user.email);
}
@Override
public int hashCode() {
return email.hashCode();
}
public class Group{
private boolean isInstructor;
private boolean isManager;
public Group(){}
public Group(boolean isInstructor, boolean isManager) {
this.isInstructor = isInstructor;
this.isManager = isManager;
}
public boolean isInstructor() {
return isInstructor;
}
public void setIsInstructor(boolean isInstructor) {
this.isInstructor = isInstructor;
}
public boolean isManager() {
return isManager;
}
public void setIsManager(boolean isManager) {
this.isManager = isManager;
}
@Override
public String toString() {
return "Group{" +
"isInstructor=" + isInstructor +
", isManager=" + isManager +
'}';
}
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", group=" + group +
'}';
}
}
答案 0 :(得分:4)
我已经明白了。从onDataChanged
方法开始,我放置了下面的代码,将数据映射到User类。
snapshot.getChildren().iterator().next().getValue(User.class)
// snapshot.getChildren().iterator().next() // returns the child of the returned data
// .getValue(User.class) // since we already retrieved the child, and we're
// // now referring to User class data, we can now
// // map the data using Firebase method (getValue(Class<T>))