杰克逊从地图中获取列表,其中键包含路径(如 "child_1_name")
jackson get list from map where key contains the path (like "child_1_name")
我们的集成点returns给我们以下结构
{
"veryImportantProperty":"some value",
"child_1_name": "Name1",
"child_1_age": 15,
"child_2_name": "Name2",
"child_2_age": 18
}
我们想将其解析为以下内容类:
class Child {
@NotEmpty
private String name;
@NotNull
private Integer age;
}
class Wrapper{
@NotEmpty
private String veryImportantProperty;
@Valid
private List<Child> children;
}
Jackson 有没有 plugin/configuration 可以帮我做这个?
谢谢
您可以通过扩展 StdDeserializer:
来定义自定义 Deserializer
class WrapperDeserializer extends StdDeserializer<Wrapper> {
public WrapperDeserializer() {
this(null);
}
public WrapperDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Wrapper deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
String veryImportantProperty = node.get("veryImportantProperty").asText();
List<Child> children = new ArrayList<Child>();
int iChild = 1;
Child child;
while(node.has("child_"+iChild+"_name")) {
child = new Child();
child.setName(node.get("child_"+iChild+"_name").asText());
child.setAge(node.get("child_"+iChild+"_age").asInt());
children.add(child);
iChild++;
}
Wrapper wrapper = new Wrapper();
wrapper.setVeryImportantProperty(veryImportantProperty);
wrapper.setChildren(children);
return wrapper;
}
}
并用 @JsonDeserialize 注释您的 Wrapper
class 以使用您的自定义反序列化器
@JsonDeserialize(using = WrapperDeserializer.class)
class Wrapper {
...
}
然后你可以使用ObjectMapper.readValue
方法在一行中反序列化
ObjectMapper mapper = new ObjectMapper();
Wrapper wrapper = null;
try {
wrapper = mapper.readValue(json, Wrapper.class);
} catch (Exception e) {
System.out.println("Something went wrong:" + e.getMessage());
}
我们的集成点returns给我们以下结构
{
"veryImportantProperty":"some value",
"child_1_name": "Name1",
"child_1_age": 15,
"child_2_name": "Name2",
"child_2_age": 18
}
我们想将其解析为以下内容类:
class Child {
@NotEmpty
private String name;
@NotNull
private Integer age;
}
class Wrapper{
@NotEmpty
private String veryImportantProperty;
@Valid
private List<Child> children;
}
Jackson 有没有 plugin/configuration 可以帮我做这个?
谢谢
您可以通过扩展 StdDeserializer:
来定义自定义Deserializer
class WrapperDeserializer extends StdDeserializer<Wrapper> {
public WrapperDeserializer() {
this(null);
}
public WrapperDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Wrapper deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
String veryImportantProperty = node.get("veryImportantProperty").asText();
List<Child> children = new ArrayList<Child>();
int iChild = 1;
Child child;
while(node.has("child_"+iChild+"_name")) {
child = new Child();
child.setName(node.get("child_"+iChild+"_name").asText());
child.setAge(node.get("child_"+iChild+"_age").asInt());
children.add(child);
iChild++;
}
Wrapper wrapper = new Wrapper();
wrapper.setVeryImportantProperty(veryImportantProperty);
wrapper.setChildren(children);
return wrapper;
}
}
并用 @JsonDeserialize 注释您的 Wrapper
class 以使用您的自定义反序列化器
@JsonDeserialize(using = WrapperDeserializer.class)
class Wrapper {
...
}
然后你可以使用ObjectMapper.readValue
方法在一行中反序列化
ObjectMapper mapper = new ObjectMapper();
Wrapper wrapper = null;
try {
wrapper = mapper.readValue(json, Wrapper.class);
} catch (Exception e) {
System.out.println("Something went wrong:" + e.getMessage());
}