我认为您应该尝试 annotation com.fasterxml.jackson.annotation.JsonProperty
。我将在下面提供一个简短的示例。
示例 YAML 文件:
---
"42": "some value"
数据传输对象类:
public class Entity {
@JsonProperty("42")
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
解析器:
public class Parser {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Entity entity = mapper.readValue(new File("src/main/resources/sample.yml"), Entity.class);
System.out.println(entity.getValue());
}
}
控制台输出应该是:some value
.
PS我使用以下依赖项对其进行了测试:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>