如何将 firebase 数据 (snake_case) 转换为 Java 对象 (camelCase)

Efe*_*dak 2 java android type-conversion firebase firebase-realtime-database

我想将我的 firebase 数据对象映射到我的 pojo。但是,我的 firebase 对象属性名称是蛇形大小写,例如;“用户名”。我想在我的 pojo 上使用 camelCase,例如;“用户名”

我找到了这样一个漂亮的答案,但是,我找不到任何关于snake_case 到camelCase 映射的示例。

我的 pojo;

@SerializedName("content")
private String content;
@SerializedName("user_name")
private String userName;
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码行进行映射。'content' 匹配没有问题(有或没有 @SerializedName 注释),但 userName 保持为空。

Story story = storySnapshot.getValue(Story.class);

这也是混淆的一个问题。是否有一种优雅的方式将数据与 pojo 匹配?

Efe*_*dak 6

问题是@SerializedName 注释。Firebase 有自己的注释,即@PropertyName。

注意 getter 名称很重要,因为注释也关心它的名称。财产也必须是公共的。

有一个关于这个的完美答案 这个链接。

我的 pojo 的最终状态;

@PropertyName("content")
public String content;
@PropertyName("user_name")
public String userName;
Run Code Online (Sandbox Code Playgroud)