[英]How can I deserialize a nested wrapped String in Jackson?
我有一个 JSON 字符串,其中包含一个嵌套和包装的 JSON 字符串。 我想使用 Jackson 反序列化它,但我不确定如何。 这是一个示例 bean:
@JsonIgnoreProperties(ignoreUnknown = true)
public class SomePerson {
public final String ssn;
public final String birthday;
public final Address address;
@JsonCreator
public SomePerson(
@JsonProperty("ssn") String ssn,
@JsonProperty("birthday") String birthday,
@JsonProperty("data") Address address,
@JsonProperty("related") List<String> related) {
this.ssn = ssn;
this.birthday = birthday;
this.address = address;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Address {
public final String city;
public final String country;
@JsonCreator
public Address(
@JsonProperty("city") String city,
@JsonProperty("country") String country) {
this.city = city;
this.country = country;
}
}
}
JSON 字符串如下所示:
{
ssn: "001",
birthday: "01.01.2020",
address: "{ city: \"London\", country: \"UK\" }"
}
虽然我之前已经反序列化了 nsted 对象 - 当 object 是一个包装字符串时,我对如何执行此操作感到很迷茫。
当内部 object 转义JSON String
时,我们需要将其反序列化“两次”。 第一次在 root JSON Object
被反序列化时运行,第二次我们需要手动运行。 为此,我们需要实现自定义反序列化器,它实现com.fasterxml.jackson.databind.deser.ContextualDeserializer
接口。 它可能看起来像这样:
class FromStringJsonDeserializer<T> extends StdDeserializer<T> implements ContextualDeserializer {
/**
* Required by library to instantiate base instance
* */
public FromStringJsonDeserializer() {
super(Object.class);
}
public FromStringJsonDeserializer(JavaType type) {
super(type);
}
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getValueAsString();
return ((ObjectMapper) p.getCodec()).readValue(value, _valueType);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
return new FromStringJsonDeserializer<>(property.getType());
}
}
我们需要用这个 class 来注释我们的属性:
@JsonDeserialize(using = FromStringJsonDeserializer.class)
public final Address address;
从现在开始,您应该能够将JSON
有效负载反序列化到您的POJO
model。
也可以看看:
Address addObject = mapper.readValue(root.getString("address"), Address.class);
其中root
是您的主要JSONObject
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.