I'm trying to map directly the output of a Jsonpath to a list of POJO. I'm using Jackson as a mapping provider.
我正在尝试将Jsonpath的输出直接映射到POJO列表。我正在使用杰克逊作为地图提供商。
Jsonpath output:
{
"actions" : [
{
"parameterDefinitions" : [
{
"defaultParameterValue" : {
"name" : "PARAM1",
"value" : ""
},
"description" : "Type String",
"name" : "PARAM1",
"type" : "StringParameterDefinition"
},
{
"defaultParameterValue" : {
"name" : "PARAM3",
"value" : ""
},
"description" : "Type String",
"name" : "PARAM3",
"type" : "StringParameterDefinition"
}
]
}
]
}
JobParameter.java (the POJO in which I'd like to map):
JobParameter.java(我要映射的POJO):
public class JobParameter {
private String description;
private String name;
private String type;
// public getters & setters
Jsonpath initialization:
Configuration conf = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.build();
List<JobParameter> jobParameters = JsonPath
.using(conf)
.parse(jsonpathOutput)
.read("$.actions[0].parameterDefinitions[0:]", List.class);
Using the above code, I always get a Map. See below the result of a toString() on this map:
使用上面的代码,我总是得到一个Map。请参阅下面这张地图上toString()的结果:
[{defaultParameterValue={name=PARAM1, value=}, description=Type String, name=PARAM1, type=StringParameterDefinition}, {defaultParameterValue={name=PARAM3, value=}, description=Type String, name=PARAM3, type=StringParameterDefinition}]
Note that when I try to map the Jsonpath output to a single object, the deserialization works fine:
请注意,当我尝试将Jsonpath输出映射到单个对象时,反序列化工作正常:
Configuration conf = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.build();
JobParameter singleJobParameter = JsonPath
.using(conf)
.parse(jsonpathOutput)
.read("$.actions[0].parameterDefinitions[0]", JobParameter .class);
In the example above, the singleJobParameter instance is well created and filled.
在上面的示例中,单个JobParameter实例已很好地创建和填充。
Am I missing something? Thanks!
我错过了什么吗?谢谢!
3 个解决方案
#1
You must use a TypeRef for this. In your case you must also use the @JsonIgnoreProperties annotation.
您必须使用TypeRef。在您的情况下,您还必须使用@JsonIgnoreProperties批注。
Configuration conf = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider())
.build();
TypeRef<List<JobParameter>> type = new TypeRef<List<JobParameter>>(){};
List<JobParameter> jobParameters = JsonPath
.using(conf)
.parse(json)
.read("$.actions[0].parameterDefinitions[0:]", type);
Note that this is not supported by all JsonMappingProviders.
请注意,并非所有JsonMappingProviders都支持此功能。
#2
<dependency>
<groupId>com.github.jsurfer</groupId>
<artifactId>jsurfer-simple</artifactId>
<version>1.2.2</version>
</dependency>
With JsonSurfer, you can achieve this by simply two lines:
使用JsonSurfer,您只需两行即可实现此目的:
JsonSurfer jsonSurfer = JsonSurfer.jackson();
Collection<JobParameter> parameters = jsonSurfer.collectAll(json, JobParameter.class, "$.actions[0].parameterDefinitions[*]");
And don't forget to ignore the unused "defaultParameterValue" in your POJO.
并且不要忘记忽略POJO中未使用的“defaultParameterValue”。
@JsonIgnoreProperties({"defaultParameterValue"})
private static class JobParameter {
private String description;
private String name;
private String type;
#3
Using a wrapper POJO solved the problem.
使用包装器POJO解决了这个问题。
@JsonIgnoreProperties(ignoreUnknown = true)
public class JobParametersWrapper {
private List<JobParameter> parameterDefinitions;
public List<JobParameter> getParameterDefinitions() {
return parameterDefinitions;
}
public void setParameterDefinitions(List<JobParameter> parameterDefinitions) {
this.parameterDefinitions = parameterDefinitions;
}
}
#1
You must use a TypeRef for this. In your case you must also use the @JsonIgnoreProperties annotation.
您必须使用TypeRef。在您的情况下,您还必须使用@JsonIgnoreProperties批注。
Configuration conf = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider())
.build();
TypeRef<List<JobParameter>> type = new TypeRef<List<JobParameter>>(){};
List<JobParameter> jobParameters = JsonPath
.using(conf)
.parse(json)
.read("$.actions[0].parameterDefinitions[0:]", type);
Note that this is not supported by all JsonMappingProviders.
请注意,并非所有JsonMappingProviders都支持此功能。
#2
<dependency>
<groupId>com.github.jsurfer</groupId>
<artifactId>jsurfer-simple</artifactId>
<version>1.2.2</version>
</dependency>
With JsonSurfer, you can achieve this by simply two lines:
使用JsonSurfer,您只需两行即可实现此目的:
JsonSurfer jsonSurfer = JsonSurfer.jackson();
Collection<JobParameter> parameters = jsonSurfer.collectAll(json, JobParameter.class, "$.actions[0].parameterDefinitions[*]");
And don't forget to ignore the unused "defaultParameterValue" in your POJO.
并且不要忘记忽略POJO中未使用的“defaultParameterValue”。
@JsonIgnoreProperties({"defaultParameterValue"})
private static class JobParameter {
private String description;
private String name;
private String type;
#3
Using a wrapper POJO solved the problem.
使用包装器POJO解决了这个问题。
@JsonIgnoreProperties(ignoreUnknown = true)
public class JobParametersWrapper {
private List<JobParameter> parameterDefinitions;
public List<JobParameter> getParameterDefinitions() {
return parameterDefinitions;
}
public void setParameterDefinitions(List<JobParameter> parameterDefinitions) {
this.parameterDefinitions = parameterDefinitions;
}
}