问题描述
我从互联网上检索一个JSON字符串;像大多数JSON我看到它包括由下划线分隔的长键。基本上,我的目标是将JSON反序列化为java对象,但我不在java代码中使用下划线。
I retrieve a JSON string from internet; like most JSON I've seen it includes long keys that are separated by underscores. Essentially, my goal is to deserialize JSON into java-objects, but I don't use underscores in java-code.
例如,我可能有一个用户
带驼峰的 firstName
字段,同时我需要以某种方式告诉杰克逊映射 first_name
从JSON到 firstName
类字段的密钥。有可能吗?
For instance, I might have a User
class with firstName
field in camel-case, simultaneously I need somehow to tell Jackson to map first_name
key from JSON to firstName
class field. Is it possible?
class User{
protected String firstName;
protected String getFirstName(){return firstName;}
}
推荐答案
您应该在要更改默认名称映射的字段上使用 @JsonProperty
。
You should use the @JsonProperty
on the field you want to change the default name mapping.
class User{
@JsonProperty("first_name")
protected String firstName;
protected String getFirstName(){return firstName;}
}
更多info:
这篇关于杰克逊克服了下划线,支持骆驼案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!