繁体   English   中英

将Yaml文件转换为Java对象

[英]Convert Yaml file to java objects

我有如下的Yaml文件

Network:
   Input 1:
      - profile : "1"
      - size: 2
   Input 2:
      - profile : "1"
      - size: 3
   Input 1:
      - profile :"2"
      - size:100
   Input 2:
      - name :"N"
      - source: "test"

并正在使用以下依赖项将其转换为类对象

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;


ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Tiles tiles = mapper.readValue(new File(args[0]), Tiles.class);

我的瓷砖课如下

    public class Tiles {

      @JsonProperty
      private Network network;
      public Network getNetwork() {
        return network;
      }
      public void setNetwork(Network network) {
        this.network = network;
      }
      }
public class Network {

  public Network() {
    super();
  }

  @JsonProperty
  private List<String> input;

  public List<String> getInput() {
    return input;
  }

  public void setInput(List<String> input) {
    this.input= input;
  }

但是当使用objectMapper解析yaml文件时出现如下问题

com.fasterxml.jackson.databind.JsonMappingException:

我想按顺序处理yaml文件,即使Input被不同的名称替换,我的意思是我必须先获取Input 1然后Input 2然后依此类推

任何帮助表示赞赏。

提前致谢

@JsonProperty用于@JsonProperty数据,即@JsonProperty("Network").

@JsonProperty("Network")
private List<RoadNetwork> roadnetwork;
//getter and setter

暂无
暂无

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

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