Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON ObjectMapper.readvalue

Tags:

java

json

jackson

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.

like image 775
chipmunk Avatar asked Apr 01 '26 13:04

chipmunk


1 Answers

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}

like image 195
Nikhil Talreja Avatar answered Apr 04 '26 02:04

Nikhil Talreja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!