Jackson YAML reader 没有正确解析 yaml 文件
Jackson YAML reader not parsing yaml file properly
我正在尝试使用 Jackson 解析 YAML 文件。请在下面找到相关文件。
Yaml 文件:
params:
- param-one : abcd
- param-two : abcd
- param-three : abcd
- param-four : abcd
型号:
@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;
}
}
我正在尝试读取 yaml 内容如下:
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
ParamsMain paramsMain = objectMapper.readValue(<PATH_OF_YAML_FILE>, ParamsMain.class);
但是当我尝试打印解析后的数据时,我得到的是:
[{
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
}]
我怎样才能读取它以获得以下输出,而不是我收到的数组?
param-one: abcd
param-two: abcd
param-three: abcd
param-four: abcd
所以我能够找到存储值的正确方法。我不得不将 POJO 更改为以下内容:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ParamsMain {
@JsonProperty("params")
private Map<String, String>[] params;
}
这帮助我以下列格式存储数据:
{{param-one=abcd},{param-two=abcd},{param-three=abcd},{param-four=abcd}}
我使用以下代码打印正在解析的内容:
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));
我正在尝试使用 Jackson 解析 YAML 文件。请在下面找到相关文件。
Yaml 文件:
params:
- param-one : abcd
- param-two : abcd
- param-three : abcd
- param-four : abcd
型号:
@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;
}
}
我正在尝试读取 yaml 内容如下:
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
ParamsMain paramsMain = objectMapper.readValue(<PATH_OF_YAML_FILE>, ParamsMain.class);
但是当我尝试打印解析后的数据时,我得到的是:
[{
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
}]
我怎样才能读取它以获得以下输出,而不是我收到的数组?
param-one: abcd
param-two: abcd
param-three: abcd
param-four: abcd
所以我能够找到存储值的正确方法。我不得不将 POJO 更改为以下内容:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ParamsMain {
@JsonProperty("params")
private Map<String, String>[] params;
}
这帮助我以下列格式存储数据:
{{param-one=abcd},{param-two=abcd},{param-three=abcd},{param-four=abcd}}
我使用以下代码打印正在解析的内容:
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));