从 YAML 文件解析对象列表 [英] Parse a List of objects from YAML file
本文介绍了从 YAML 文件解析对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 Yaml 文件解析为对象列表,但出现以下错误.
I am attempting to parse my Yaml file to a list of object But I am getting the following error.
Method threw 'com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException' exception.
我的 Java 代码:
My Java code:
public class ViewResultsModel
{
@JsonProperty
List<FileModel> files;
public ViewResultsModel()
{
}
public ViewResultsModel(List<FileModel> files)
{
this.files = files;
}
// getters and setters omitted
}
public class FileModel
{
@JsonProperty
String fileType;
@JsonProperty
String destination;
@JsonProperty
String filePath;
// getters and setters omitted
}
public static void main(String[] args) throws IOException
{
File file = new File("Template.yaml");
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
mapper.readValue(file, ViewResultsModel.class);
}
YAML 文件:
file:
fileType: TXT
fileDestination: there
filePath: C:/
file:
fileType: PDF
fileDestination: here
filePath: C:/
我想读取 Yaml 并创建一个 FileModel 对象列表
I want to read the Yaml and create a List of FileModel objects
推荐答案
您的 YAML 对象具有您的 Java 类不知道的成员 fileDestination
.而不是
Your YAML objects have a member fileDestination
which your Java class does not know. Instead of
@JsonProperty
String destination;
试试
@JsonProperty("fileDestination")
String destination;
顺便说一句,您的输入不是合法的 YAML.您两个 项目命名file
,其中只有一个是可能的.您想建立一个列表吗?
BTW, your input is not legal YAML. You two items name file
where only one is possible. Did you want to build a list?
这篇关于从 YAML 文件解析对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文