Have an array, when the size is 1, the json data I received does NOT contains []; like
{"firstname":"tom"}
when the size is larger than 1, the data I received contains [], like
[{"firstname":"tom"},{"firstname":"robert"}]
Currently my class contains an array property
String[] firstname;
//getter setter omit here
Code to handle this likes
ObjectMapper mapper = new ObjectMapper();
MyClass object = mapper.readValue(json, MyClass.class);
When the size is larger than 1, the deserialization works. However when size is 1, the deserialization failed.
I am currently using jackson, any solution for this problem?
I am wondering if jackson/gson or any other library can handle this?
For Jackson specifically, your best bet would to first bind to a JsonNode or Object, like:
and then check what you got, bind again:
But I think JSON you are getting is bad — why would you return an object, or array, intead of just array of size 1? — so if at all possible, I’d rather fix JSON first. But if that is not possible, this would work.