简体   繁体   中英

Deserialize JSON With Only Canonical Type String Using Jackson

I have a JSON string

{
    "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 .)

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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