[英]Unable to map a JSON field with Hyphen to the Java Object field
The Java POJO is like this: Java POJO 是这样的:
import javax.validation.Valid;
import com.fasterxml.jackson.annotation.JsonProperty;
Class MyClass{
@JsonProperty(value = "config-meta-info")
@Valid
private ConfigMetaInformation configMetaInfo;
@JsonProperty(value = "name")
@Valid
private String name;
public MyClass(){}
public MyClass(String name,ConfigMetaInformation configMetaInfo){
this.name=name;
this.configMetaInfo=configMetaInfo;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("config-meta-info")
public ConfigMetaInformation getConfigMetaInfo() {
return configMetaInfo;
}
@JsonProperty("config-meta-info")
public void setConfigMetaInfo(ConfigMetaInformation configMetaInfo) {
this.configMetaInfo= configMetaInfo;
}
}
I am Using the JSON as below:我正在使用 JSON,如下所示:
{
"name":"abc",
"config-meta-info":"someInfo"
}
But when I try to get the Data from the MongoDB document , I am seeing the config-meta-info as null.但是当我尝试从 MongoDB 文档中获取数据时,我看到 config-meta-info 为空。 Am I missing anything to handle this kebab-case key?
我是否缺少任何东西来处理这个烤肉串钥匙?
I could be wrong in the case of MongoDB but in other JSON based databases, they don't allow hyphenation in the field/key, underscores are usually preferred.在 MongoDB 的情况下我可能是错的,但在其他基于 JSON 的数据库中,它们不允许在字段/键中使用连字符,通常首选下划线。 Instead of
config-meta-info
, try config_meta_info
.而不是
config-meta-info
,尝试config_meta_info
。
The structure which you showed:你展示的结构:
{
name:"abc",
config-meta-info:"someInfo"
}
is not a JSON.不是 JSON。 Specification RFC-8259 defines all types (6) but what is
name
or config-meta-info
?规范RFC-8259定义了所有类型 (6) 但什么是
name
或config-meta-info
? It can be JavaScript, but not a JSON.它可以是 JavaScript,但不能是 JSON。
The proper JSON:正确的 JSON:
{
"name":"abc",
"config-meta-info":"someInfo"
}
And you can use hyphen without limitations.您可以无限制地使用连字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.