简体   繁体   English

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

[英]How to convert firebase data (snake_case) to Java object (camelCase)

I want to map my firebase data object to my pojo. 我想将我的firebase数据对象映射到我的pojo。 However, my firebase object property name is snake case, such as; 但是,我的firebase对象属性名是snake case,例如; "user_name". “用户名”。 I want to use camelCase on my pojo, such as; 我想在我的pojo上使用camelCase,例如; "userName" “用户名”

I found beautiful answers like this one, however, I couldn't find any sample about snake_case to camelCase mapping. 我找到了像这样的漂亮答案,然而,我找不到任何关于snake_case的样本来camelCase映射。

My pojo; 我的pojo;

@SerializedName("content")
private String content;
@SerializedName("user_name")
private String userName;

I'm using the following line of code for mapping. 我正在使用以下代码行进行映射。 'content' matches with no problem(with or without @SerializedName annotation) but userName stays as null. 'content'匹配没有问题(有或没有@SerializedName注释)但userName保持为null。

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

That is also an issue for obfuscation. 这也是混淆的问题。 Is there an elegant way to match the data to pojo? 有没有一种优雅的方式将数据与pojo相匹配?

The problem was @SerializedName annotation. 问题是@SerializedName注释。 Firebase has its own annotation, which is @PropertyName. Firebase有自己的注释,即@PropertyName。

It is important to be careful about getter name because annotation cares about its name too. 注意getter名称非常重要,因为注释也关注它的名称。 The property must be public too. 该物业也必须公开。

There is a perfect answer about that on this link. 这个链接上有一个完美的答案。

Final state of my pojo; 我的pojo的最终状态;

@PropertyName("content")
public String content;
@PropertyName("user_name")
public String userName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webclient + Jackson:如何设置反序列化将snake_case转换为camelCase? - Webclient + Jackson: how to setup deserialization to convert snake_case into camelCase? Java中从snake_case到camelCase - From snake_case to camelCase in Java JAVA - 不使用 ObjectMapping 和 SetPropertyNamingStrategy 将 CamelCase 转换为 Snake_Case - SNAKE_CASE - JAVA - Not converting CamelCase to Snake_Case using ObjectMapping and SetPropertyNamingStrategy - SNAKE_CASE 如何在Java中将snake_case JSON转换为嵌套JSON? - How to convert a snake_case JSON to nested JSON in java? 除非明确指定,否则如何在默认情况下将 Spring Data JPA 从 camelCase 命名为 snake_case? - How to make Spring Data JPA by default naming from camelCase to snake_case unless explicitly specified? SpringBoot:如何将蛇案例转换为骆驼案例 - SpringBoot: How to convert snake case to camelCase 如何 map Hibernate 实体字段使用camelCase到snake_case(下划线)数据库标识符 - How to map Hibernate entity fields using camelCase to snake_case (underscore) database identifiers 如何解析 SnakeYAML 中的 snake_case 属性 - How to parse snake_case properties in SnakeYAML 如何使用Jackson将蛇案Yaml映射到camelcase Java字段 - How to map snake case yaml to camelcase java fields with Jackson 泽西岛MOXy无法解析snake_case - Jersey MOXy not parsing snake_case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM