问题描述
我有一个包含嵌套和包装的 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 对象 - 当对象是一个包裹的字符串时,我很不知道如何做到这一点。
解决方法
当内部对象被转义 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());
}
}
我们需要用这个类来注释我们的属性:
@JsonDeserialize(using = FromStringJsonDeserializer.class)
public final Address address;
从现在起,您应该可以将 JSON
以上的有效负载反序列化到您的 POJO
模型中。
另见:
- How to inject dependency into Jackson Custom deserializer
- Is it possible to configure Jackson custom deserializers at class level for different data types?
readValue(String,Class)
应该可以工作:
Observable<T>[]
其中 Address addObject = mapper.readValue(root.getString("address"),Address.class);
是您的主要 root
。