How to deserialize an array of two lists in Jackson
I have an array of two lists. Each list contains objects. The objects in the first list are different from the objects in the second list. In JSON it looks like this:
[
{
"domains":
[
{
"attrb1": "aaaa",
"attrb2": "bbbb"
},
{
"attrb1": "cccc",
"attrb2": "dddd"
}
]
},
{
"terms":
[
{
"attrb1": "cccc",
"attrb2": "dddd"
}
]
}
]
I saved this JSON string in the initial-elements-v02.json file, and I was thinking about something similar to deserialize it:
final ObjectMapper mapper = new ObjectMapper();
List<List<Object>> glossaryElements = null;
try {
glossaryElements = mapper.readValue(
arg0.resourceAsStream("initial-elements-v02.json"),
new TypeReference<ListList<<Object>>>() {
});
Thank!
Evgeny
+3
source to share
1 answer
According to data binding with Generics , I would build from innermost to outermost
Map<String, String>
List<Map<String, String>>
Map<String, List<Map<String, String>>>
List<Map<String, List<Map<String, String>>>>
and then use this
List<Map<String, List<Map<String, String>>>> glossaryElements = null;
glossaryElements = mapper.readValue(
arg0.resourceAsStream("initial-elements-v02.json"),
new TypeReference<List<Map<String, List<Map<String, String>>>>>() {});
If initial-elements-v02.json
is the filename, I think you can also use
new File("initial-elements-v02.json")
+1
source to share