Skip to content
Advertisement

Deserializing json to pojo where json field has different data types

I’m trying to deserialize json to java pojo and I can’t wrap my head around how should I begin to create a java object that could be used to deserialize json like this:

JavaScript

I’m working with jackson and looking to use something like this: MyPojo response = mapper.readValue(jsonString, MyPojo.class)

The biggest struggle is those “value” elements, where some fields contain simple String and in other cases have an array of elements

I’ve tried to create an abstract class for the element “value”, and then 2 more classes that would extend abstract class. But that didn’t work

Advertisement

Answer

The best way is create a custom type resolver:

JavaScript

running with

JavaScript

the object contains all required values

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement