繁体   English   中英

解析来自YAML文件的对象列表

[英]Parse a List of objects from YAML file

我试图将Yaml文件解析为对象列表,但出现以下错误。

Method threw 'com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException' exception.

我的Java代码:

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对象的列表

您可以在下面的行中指定ViewResultsModel类:

mapper.readValue(file, ViewResultsModel.class);

但是,要映射的实际字段在FileModel类中

您的YAML对象具有Java类不知道的成员fileDestination 代替

@JsonProperty
String destination;

尝试

@JsonProperty("fileDestination")
String destination;

顺便说一句,您输入的内容不是合法的YAML。 您有两个项目的名称file ,其中只有一个是可能的。 您要建立清单吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM