如何将数据从DataSnapshot中解析到类工作中?

发布于 2025-01-24 03:17:04 字数 1025 浏览 0 评论 0原文

我曾经使用map< string,object> map =(map< string,object>)datasnapshot.getValue();> ,

但是当我在youtube上观看教程时,我看到了那个家伙使用this user user user user user user user = dataSnapshot的类别来解析datasnapshot。 getValue(user.class);,现在我有很多问题。

这是如何工作的?

我的意思是,如果班级用户的成员与快照不同,该怎么办?

例如

snapshot:
-User
 |-Name:jojo
 |-Age:19

User Class fields:
 boolean a;
 float x;
 int c;

,如果类用户的字段与快照上的putter的顺序不同,并且具有不同的大写字母,或者班级有一个缺失字段怎么办?

我的第二个问题的示例:

snapshot:
-User
 |-Name:jojo
 |-Age:19

User Class fields:
 int age;
 String name;

我已经阅读了firebase文档https://firebase.google.com/docs/reference/RANDROID/android/com/google/firebase/firebase/database/datasnapsnapsnapshot#getvalue(Java.lang.lang.class) 但是它没有回答我的两个问题,它只是说班级必须有一个空的构造函数和每个字段

编辑的getter:

snapshot:
-User
 |-Followers
  |-id:xxxxxx
  |-id:xxxxxx
 |-Name:jojo
 |-Age:19

User Class fields:
 int age;
 String name;

I used to parse dataSnapshots using maps like this Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();

But then when I was watching a tutorial on YouTube I saw the guy parsing the dataSnapshot using a class like this User user = dataSnapshot.getValue(User.class); and now I have a lot of questions.

How does that work?

I mean what if the class User had different members than the snapshot would it give us an error?

For example

snapshot:
-User
 |-Name:jojo
 |-Age:19

User Class fields:
 boolean a;
 float x;
 int c;

what if the fields of the class User where putter in a different order than the ones on the snapshot and with different capitalization or what if the class had one missing field?

Example for my second question:

snapshot:
-User
 |-Name:jojo
 |-Age:19

User Class fields:
 int age;
 String name;

I have read the firebase docs https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot#getValue(java.lang.Class)
but it does not answer my two questions it just says the class must have an empty constructor and a getter for each field

EDIT:

snapshot:
-User
 |-Followers
  |-id:xxxxxx
  |-id:xxxxxx
 |-Name:jojo
 |-Age:19

User Class fields:
 int age;
 String name;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2025-01-31 03:17:04

Firebase尝试使用Javabean命名约定的数据库中的JSON数据中的属性映射。映射到数据快照的最简单类是:

public static class User {
  public String Name;
  public long Age;
}

此类为数据库中的每个属性都有一个公共字段,编译器将生成一个默认的无差异构造函数,这是Firebase为创建用户数据的对象。


一个更常见的模式是为属性创建Getters和/或Setter。有了所有这些属性的所有这些属性,您将获得:

public static class User {
  String Name;
  long Age;

  public String getName() { return Name }
  public void getName(String name) { Name = name }
  public long getAge() { return Age }
  public void setSage(long age) { Age = age }
}

现在,使用这些领域本身不再需要公开,并且在需要的情况下,我们可以在需要的情况下进行额外的工作。但是,现在将使用Javabean命名转换从这些Geters和Setters确定属性的名称,这导致属性name and code> age ,其外壳与您的外壳不同数据库。

为了解决此问题,您可以在代码中提供注释,每个字段的数据库属性是:

public static class User {
  String Name;
  long Age;

  @PropertyName("Name")
  public String getName() { return Name }
  @PropertyName("Name")
  public void getName(String name) { Name = name }
  @PropertyName("Age")
  public long getAge() { return Age }
  @PropertyName("Age")
  public void setSage(long age) { Age = age }
}

现在,firebase映射器将读取propertyname注释,并能够在数据库中正确映射到数据库和该数据之间再次在您的对象中。


如果您的同类比数据库中的数据具有更多的属性(因此公共字段或Getters/setters),那么当这些属性从数据库中读取数据时,这些属性将不足。写入数据库时​​,其他属性将写入数据库。

如果要防止这种情况,则可以使用 @ignoreextraproperties 注释

另请参见:

Firebase tries to map between the properties in the JSON data from the database and those in your class using JavaBean naming conventions. The simplest possible class to map to your data snapshot would be:

public static class User {
  public String Name;
  public long Age;
}

This class has a public field for each property in the database, and the compiler will generate a default, no-argument constructor, which is what Firebase needs in order to create User objects for the data.


A more common patter is to create getters and/or setters for the properties. With all of those for both of your properties, you'd get:

public static class User {
  String Name;
  long Age;

  public String getName() { return Name }
  public void getName(String name) { Name = name }
  public long getAge() { return Age }
  public void setSage(long age) { Age = age }
}

Now with these the fields themselves no longer have to be public, and we can do extra work if needed in the get and set methods. But the name of the properties will now be determined from those getters and setters using JavaBean naming conversions, which leads to properties name and age with a different casing than what you have in the database.

To solve this, you can provide annotations in your code what the database properties for each field are:

public static class User {
  String Name;
  long Age;

  @PropertyName("Name")
  public String getName() { return Name }
  @PropertyName("Name")
  public void getName(String name) { Name = name }
  @PropertyName("Age")
  public long getAge() { return Age }
  @PropertyName("Age")
  public void setSage(long age) { Age = age }
}

Now the Firebase mapper will read the PropertyName annotations and be able to correctly map between the data from the database and that in your objects again.


If your class has more properties (so either public fields or getters/setters) than the data from the database, those properties are left unpopulated when it reads data from the database. When writing to the database, the additional properties will be written to the database.

If you want to prevent this, you can use the @IgnoreExtraProperties annotation.

Also see:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文