Deserializing multiple neted data structure

10 views
Skip to first unread message

Ralf Heydenreich

unread,
Oct 19, 2018, 7:36:42 PM10/19/18
to jackson-user
Hi all,
I have the following data structure (omitted some attributes):


{
   
"status": "success",
   
"data": {
       
"2": {
           
"80": {
               
"id": "80",
               
"bezeichnung": "a name",
               
"bookings": {
                   
"13": {
                       
"id": "13",
                       
"note": ""
                   
},
                   
"14": {
                       
"id": "14",
                       
"note": ""
                   
},
               
},
                ...
             
},
           
"159": {
               
"id": "159",
               
"bezeichnung": "another name",
               
...
           
}
       
}
   
}
}

I want to get this into a proper Java class structure like so (pseudocode):

class DataContainer {
 
List<Event> events;
}

class Event {
 
String id;
 
String bezeichnung;
 
List<Booking> bookings;
 
...
}

class Booking {
 
String id;
 
String note;
}

I've always struggled with custom deserializers but I didn't get it to work. Currently, I've solved it with AnySetter and a plain Map, but this is really ugly and unusable for larger aps. How can I deserialize such a structure in a proper way? Thanks in advance for your ideas.

Ralf.

 






Tatu Saloranta

unread,
Oct 22, 2018, 1:01:56 AM10/22/18
to jackson-user
If the structure of your data and POJOs does not really match,
databinding is not going to be easy, unfortunately.

So often the best bet would be to first bind into intermediate
representation, like JsonNode, then transform it
to a structure that is compatible with POJO, and use
`ObjectMapper.treeToValue(node, TargetType.class);`
Or it may be easy to at that point handle binding manually by
extracting data via JsonNode API.

-+ Tatu +-

Ralf Heydenreich

unread,
Oct 22, 2018, 1:02:43 PM10/22/18
to jackson-user
Hi Tatu,
I'll do this in this way. I'll get the "raw" data from the REST API and afterwards convert it into real objects. Thanks for your help.

Ralf.
Reply all
Reply to author
Forward
0 new messages