【问题标题】:Convert Yaml file to java objects将 Yaml 文件转换为 java 对象
【发布时间】:2018-03-20 11:28:20
【问题描述】:

我有如下 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);

我的 Tiles 类如下所示

    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 被不同的名称替换,我的意思是 我必须先输入输入 1,然后输入输入 2,然后以此类推

感谢任何帮助。

提前致谢

【问题讨论】:

标签: java yaml


【解决方案1】:

@JsonProperty 与来自resonse 的数据一起使用,即@JsonProperty("Network").

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

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 2017-08-16
    • 2018-11-23
    • 1970-01-01
    • 2013-09-09
    • 2016-06-02
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多