I'm trying to parse json with invalid structure which has duplicate keys using Jackson
library. If a json has duplicate keys, I would like to extract them as a Collection
.
Example of what I'm trying to parse (the actual json that I'm trying to parse comes from Wireshark
json export):
{
"a": "a",
"a": {
"b": {
},
"b": true
}
}
However, since this json has duplicate keys, only the last value is retained:
JsonNode tree = new ObjectMapper().readTree(json);
System.out.println(tree); // {"a":{"b":true}}
I've also tried the Guava
module which has Multimap
support, however it doesn't work as expected for nested json objects.
Example using Guava module for the json that I have shown before:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new GuavaModule());
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Multimap read = mapper.readValue(json, Multimap.class);
System.out.println(read); // {a=[a, {b=true}]}
How should I tackle this problem using Jackson
library? Are there any other libraries which would support parsing of such json structure for java?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…