I have a entity object like this:
class Person{
private String name;
private String class;
private String semester;
}
Now I have a map like this: Map<String,String>
{
"name":"Giri"
"class":"12"
"semester":"C"
}
Now I want to convert this map to a Person object where each field in object corresponds to that field in map.
How can I do this?
Thanks
You can write your custom mapper for this.
toPerson(Map<String, String> map) {
if(map == null) {
return null;
}
Person person = new Person();
person.setName = map.get("name");
person.setClass = map.get("class");
person.setSemester = map.get("semester");
return person;
}
Or you can use jackson convertValue()
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Person person = mapper.convertValue(map, Person.class);
Apart from jackson's ObjectMapper there are DozerBeanMapper, BeanUtils, google's Gson can be used for same purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With