简体   繁体   English

使用 Jackson 仅使用规范类型字符串反序列化 JSON

[英]Deserialize JSON With Only Canonical Type String Using Jackson

I have a JSON string我有一个 JSON 字符串

{
    "type": "com.example.model.Person",
    "data": {
        "firstName": "Bob",
        ...
    }
}

That is represented by the following class.这由以下类表示。

public class Container<T> {
    private String type;
    private T data;

    // Getters and Setters
}

(I've even tried just removing the generic type and replacing it with Object .) (我什至尝试删除泛型类型并将其替换为Object 。)

I have tried the following:我尝试了以下方法:

  • new ObjectMapper().readValue(json, Class.forName(canonical))
  • new ObjectMapper().readValue(json, TypeFactory.defaultInstance().constructFromCanonical(canonical))

I need to be able to deserialize the JSON string into a Container<T> instance with only the given canonical type string.我需要能够将 JSON 字符串反序列化为一个Container<T>实例,仅包含给定的规范类型字符串。 How can this be done?如何才能做到这一点?

I tried the same example and its working for me我尝试了同样的例子,它对我有用

在此处输入图片说明

Is it the same you are looking for or something else是你要找的还是别的什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM