mercredi 15 avril 2015

Deserializing JSON when there are properties with collections

I have successfully used Spring & Jackson as follows,



ResponseEntity<MyClass> result = restTemplate.exchange(
MYURL, HttpMethod.POST, entity,
MyClass.class);


Here MyClass is a POJO where there is a field mapped with each field in the JSON.


And also I could map the situations where there are many JSON "objects"



ResponseEntity<MyClass[]> responseEntity = restTemplate.getForEntity(MYURL, MyClass[].class);


This parses a simple array of objects and it worked fine.


But now I am in a more complicated situation. I have a JSON like this.



{
"id": "1998",
"items": {
"1": {
"map": {
"1": "1.2",
"2": "2.5",
"3": "6.1",
"4": "5.4",
"5": "9.7",
"6": "4.23",
"7": "7.65",
"8": "1.1",
"9": "8.5",
"10": "6.2"
}
}
}
}


In the "items" array you can have another items as "2" or "3". Each of them contains a different "map". I wan't to map these information into an Object.


Obviously the array approach above does not work work since there is a property "id" there.


How can I design a POJO for this? Do I need to write a Custom Deserializer for this?


Aucun commentaire:

Enregistrer un commentaire