简体   繁体   English

Jackson YAML 阅读器未正确解析 yaml 文件

[英]Jackson YAML reader not parsing yaml file properly

I'm trying to parse a YAML file with Jackson.我正在尝试使用 Jackson 解析 YAML 文件。 Please find the relevant files below.请在下面找到相关文件。

Yaml file: yaml文件:

params:
 - param-one : abcd
 - param-two : abcd
 - param-three : abcd
 - param-four : abcd

Model:模型:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "params"
})
@Data
public class ParamsMain {

    @JsonProperty("params")
    private List<Params> params = null;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
            "param-one",
            "param-two",
            "param-three",
            "param-four"
    })
    @Data
    public static class Params {

        @JsonProperty("param-one")
        private String paramOne;

        @JsonProperty("param-two")
        private String paramTwo;

        @JsonProperty("param-three")
        private String paramThree;

        @JsonProperty("param-four")
        private String paramFour;
    }
}

I am trying to read the yaml contents as follows:我正在尝试按如下方式阅读 yaml 内容:

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
ParamsMain paramsMain = objectMapper.readValue(<PATH_OF_YAML_FILE>, ParamsMain.class);

But when I try to print the parsed data, this is what I get:但是当我尝试打印解析的数据时,这就是我得到的:

[{
    param-one: abcd
    param-two: null
    param-three: null
    param-four: null
},
{
    param-one: null
    param-two: abcd
    param-three: null
    param-four: null
},
{
    param-one: null
    param-two: null
    param-three: abcd
    param-four: null
},
{
    param-one: null
    param-two: null
    param-three: null
    param-four: abcd
}]

How can I make it read such that I get the following output, instead of the array I am receiving?我怎样才能让它读取,以便我得到以下输出,而不是我收到的数组?

param-one: abcd
param-two: abcd
param-three: abcd
param-four: abcd

So I was able to figure out the right way to store the values.所以我能够找出存储值的正确方法。 I had to change the POJO to the following:我不得不将 POJO 更改为以下内容:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ParamsMain {

    @JsonProperty("params")
    private Map<String, String>[] params;

}

This helped me store the data in the following format:这帮助我以以下格式存储数据:

{{param-one=abcd},{param-two=abcd},{param-three=abcd},{param-four=abcd}}

I used the following code to print what is being parsed:我使用以下代码打印正在解析的内容:

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
ParamsMain paramsMain = objectMapper.readValue(<PATH_OF_YAML_FILE>, ParamsMain.class);


//code to print
System.out.println(ReflectionToStringBuilder.toString(paramsMain,ToStringStyle.MULTI_LINE_STYLE));

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

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