本文介绍了转换 Map<String, String>到 POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在看 Jackson,但似乎我必须将 Map 转换为 JSON,然后将生成的 JSON 转换为 POJO.
I've been looking at Jackson, but is seems I would have to convert the Map to JSON, and then the resulting JSON to the POJO.
有没有办法将 Map 直接转换为 POJO?
Is there a way to convert a Map directly to a POJO?
推荐答案好吧,您也可以通过 Jackson 实现这一目标.(而且因为您考虑使用 jackson 似乎更舒服).
Well, you can achieve that with Jackson, too. (and it seems to be more comfortable since you were considering using jackson).
使用 ObjectMapper 的 convertValue 方法:
Use ObjectMapper's convertValue method:
final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper final MyPojo pojo = mapper.convertValue(map, MyPojo.class);无需转换成JSON字符串或其他东西;直接转换的速度要快得多.
No need to convert into JSON string or something else; direct conversion does much faster.
更多推荐
转换 Map<String, String>到 POJO
发布评论