【问题标题】:Jackson json property (string) to instanceJackson json 属性(字符串)到实例
【发布时间】:2019-05-16 15:02:44
【问题描述】:

假设我有以下 JSON:

{
  "property": "123:1234"
}

如何使用Jackson注解确保"property"的字符串值被反序列化为自定义类而不是String对象? 我浏览了他们的文档,但找不到这个特定的功能。

提前致谢。

【问题讨论】:

  • 您是否希望将您的属性存储在带有字段的某个对象中?可能被: 分隔?
  • @michalk 是的。该字符串在我们的系统中用作标识符,我想将其包装在专用于这些标识符的类中。

标签: java json jackson


【解决方案1】:

您可以为您的字段创建自定义反序列化程序。假设您要将其映射到 SomeClass 对象:

public class SomeClass {

    @JsonDeserialize(using = CustomPropertyDeserializer.class)
    private Properties property;

    public Properties getProperty() {
        return property;
    }

    public void setProperty(Properties property) {
        this.property = property;
    }
}

您使用 @JsonDeserialize 注释传递自定义反序列化器来自定义要反序列化的字段。 您的反序列化器可能如下所示:

public class CustomPropertyDeserializer extends StdDeserializer<Properties> {

    public CustomPropertyDeserializer() {
        super(Properties.class);
    }

    @Override
    public Properties deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String valueAsString = p.getValueAsString();
        String[] split = valueAsString.split(":");

        return new Properties(split[0], split[1]);
    }
}

还有自定义属性类:

public class Properties {
    private String first;
    private String second;

    public Properties(String first, String second) {
        this.first = first;
        this.second = second;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }
}

为了测试它:

    public static void main(String[] args) throws IOException {
        String s = Files.lines(Paths.get("src/main/resources/data.json")).collect(Collectors.joining());

        ObjectMapper objectMapper = new ObjectMapper();

        SomeClass someClass = objectMapper.readValue(s, SomeClass.class);

        System.out.println(someClass.getProperty().getFirst());
        System.out.println(someClass.getProperty().getSecond());

    }

然后输出是:

123
1234

因此,如何将 String 映射到您定义的某个类的所有自定义逻辑都可以放在自定义反序列化器的 deserialize 方法中。

【讨论】:

    【解决方案2】:

    首先定义你需要使用的类:

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class JsonTest{
    @JsonProperty("property")
    private String property;
    
        //define your getters and setters for the field
    

    那么你就可以使用jackson的ObjectMapper类了:

      public static <T> T extractObjectFromJson(String jsonText, Class<T> type) {
        try {
            return new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).reader().forType(type)
                    .readValue(jsonText);
        } catch (Exception e) {
            //Manage your exception here
        }
        return null;
    }
    

    因此,您只需调用方法extractobjectFromJson(//Your JSON String, JsonTest.class) 即可反序列化您的 JSON。

    【讨论】:

      猜你喜欢
      • 2013-05-09
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多