I have many JSON files, which i am parsing using Jackson. So a part of my JSON looks like
"data": {
"k": "ewt",
"e": "dwpc",
"d": 2,
"ex": 0,
"t": 3439
}
"data": {
"k": "mmm1",
"e": [{
"x": 548,
"y": 330,
"t": 35733
}, {
"x": 541,
"y": 342,
"t": 36354
}],
"min": 0,
"max": 0,
"avg": 0
}
Here, if iyou notice, in the first "data" block type of "e" is string and in the second the type is Array. My concern is only with the second type of "e" that is array, so I made my POJO beans as follows -
Class data.java
...
@JsonProperty("e")
private List<MouseDataArray> e = new ArrayList<MouseDataArray>();
some more properties .. and getters and setters ..
And
Class MouseDataArray.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"x",
"y",
"t"
})
public class MouseDataArray {
@JsonProperty("x")
private Long x;
@JsonProperty("y")
private Long y;
@JsonProperty("t")
private Long t;
.. getters and setters
}
Now the thing is I am only concerned with the property "e" of type Array, and not interested in the String type property "e". So When it parses, it throuws exception Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token Which is expected, but I want it to just skip the property e of type string rather than trying to map it to the Array type.
Thanks.
It seems that incoming JSON is badly designed, since it uses both regular String type and Object for 'e' -- most OO languages do not have real common base type for such combination. So your only bet for data-binding is to declare e
to be either java.lang.Object
(in which case it'll become either java.lang.String
or java.util.Map
), or JsonNode
("JSON Tree"). And then you will need to extract data out after binding.
Although you could implement a custom deserializer, it is often simpler to just use Object
or JsonNode
as initial type, and then handle later conversion. For example, you can convert from JsonNode
into any other type simply with:
MyValue v = mapper.treeToValue(treeNode, MyValue.class)
or, if you prefer, just get stuff out if
JsonNode first = treeNode.get(0);
MyValue v = new MyValue(first.get("x").asInt(), ...);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.