1. Overview
1.概述
Field names in JSON objects can be in a variety of formats. When we want to load those into our POJOs, we may encounter a problem where the property names in our Java code don’t match the naming convention in the JSON.
JSON对象中的字段名可以有多种格式。当我们想把这些加载到我们的POJO中时,我们可能会遇到一个问题,即我们的Java代码中的属性名称与JSON中的命名规则不一致。
In this short tutorial, we’ll see how to deserialize snake case JSON to camel case fields using Jackson.
在这个简短的教程中,我们将看到如何使用Jackson将蛇形的JSON反序列化为骆驼形字段。
2. Install Jackson
2.安装Jackson
Let’s start by adding the Jackson dependency to our pom.xml file:
让我们首先将Jackson依赖性添加到我们的pom.xml文件中。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13</version>
</dependency>
3. Deserialize With the Defaults
3.用默认值进行反序列化
Let’s consider an example User class:
让我们考虑一个例子User类。
public class User {
private String firstName;
private String lastName;
// standard getters and setters
}
Let’s try to load this JSON, which uses the Snake Case naming standard (lowercase names separated by _):
让我们试着加载这个JSON,它使用Snake Case命名标准(小写名字用_分隔)。
{
"first_name": "Jackie",
"last_name": "Chan"
}
First, we need to use ObjectMapper to deserialize this JSON:
首先,我们需要使用ObjectMapper来反序列化这个JSON。
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(JSON, User.class);
However, when we try this, we get an error:
然而,当我们尝试这样做时,我们得到一个错误。
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "first_name" (class com.baeldung.jackson.snakecase.User), not marked as ignorable (2 known properties: "lastName", "firstName"])
Unfortunately, Jackson cannot exactly match the names in the JSON to the field names in User.
不幸的是,Jackson无法将JSON中的名称与User.中的字段名称完全匹配。
Next, we’ll learn three ways to solve this problem.
接下来,我们将学习三种方法来解决这个问题。
4. Use @JsonProperty Annotation
4.使用@JsonProperty注释
We can use the @JsonProperty annotation on the fields of our class to map the fields to the exact names in our JSON:
我们可以在我们的类的字段上使用@JsonProperty注解,将字段映射到我们的JSON中的确切名称。
public class UserWithPropertyNames {
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
// standard getters and setters
}
Now we can deserialize our JSON to a UserWithPropertyNames:
现在我们可以将我们的JSON反序列化为一个UserWithPropertyNames。
ObjectMapper objectMapper = new ObjectMapper();
UserWithPropertyNames user = objectMapper.readValue(JSON, UserWithPropertyNames.class);
assertEquals("Jackie", user.getFirstName());
assertEquals("Chan", user.getLastName());
5. Use @JsonNaming Annotation
5.使用@JsonNaming注释
Next, we can use @JsonNaming annotation on the class, and all fields will be deserialized using snake case:
接下来,我们可以在类上使用@JsonNaming注解,所有的字段都将使用snake case进行反序列化。
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class UserWithSnakeStrategy {
private String firstName;
private String lastName;
// standard getters and setters
}
Then deserialize our JSON again:
然后再对我们的JSON进行反序列化。
ObjectMapper objectMapper = new ObjectMapper();
UserWithSnakeStrategy user = objectMapper.readValue(JSON, UserWithSnakeStrategy.class);
assertEquals("Jackie", user.getFirstName());
assertEquals("Chan", user.getLastName());
6. Configure the ObjectMapper
6.配置ObjectMapper
Finally, we can use the setPropertyNamingStrategy method on ObjectMapper to configure it for all serialization:
最后,我们可以使用setPropertyNamingStrategy方法对ObjectMapper进行配置,以实现所有序列化。
ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
User user = objectMapper.readValue(JSON, User.class);
assertEquals("Jackie", user.getFirstName());
assertEquals("Chan", user.getLastName());
As we see, we can now deserialize our JSON into the original User object, even though the User class doesn’t have any annotation.
正如我们所看到的,我们现在可以将我们的JSON反序列化为原始的User对象,尽管User类并没有任何注释。
We should note that there are other naming conventions (kebab case, for example), and the above solutions work with them as too.
我们应该注意到,还有其他的命名惯例(例如烤羊肉串的情况),上述解决方案对它们也适用。
7. Conclusion
7.结语
In this article, we’ve seen different ways to deserialize snake case JSON to camel case fields using Jackson.
在这篇文章中,我们已经看到了不同的方法,使用Jackson将蛇案JSON反序列化为驼案字段。
First, we explicitly named the fields. Then we set a naming strategy on the POJO itself.
首先,我们明确命名了这些字段。然后,我们在POJO本身设置了一个命名策略。
Finally, we added a global configuration to the ObjectMapper.
最后,我们给ObjectMapper添加了一个全局配置。
As always, the example code from this article can be found over on GitHub.
一如既往,本文中的示例代码可以在GitHub上找到over。