chipmunk
chipmunk

Reputation: 565

Jackson JSON ObjectMapper.readvalue

I'm going through code examples on converting the Java object to a JSON and I came across this:

HashMap<String, Object> filters = new ObjectMapper().readValue(filterStr, HashMap.class);

where

String filterStr;

sorry, but what exactly is the above line of code doing? I went through other example here. I can see that readValue() has been overridden but how can a string be converted to a HashMap? Shouldn't it be a JSON object and not a string? Thanks.

Upvotes: 3

Views: 14813

Answers (1)

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

ObjectMapper().readValue()

is overloaded to do several types of conversions.

If the filterStr is compatible to be converted to a HashMap this will method will do it.

E.g. filterStr = "{\"name\":\"Tom\", \"age\":\"25\"}"; will give a map with key-value pairs as {age=25, name=Tom}

Upvotes: 3

Related Questions