转换地图< String,String>到POJO [英] Convert a Map<String, String> to a POJO

查看:131
本文介绍了转换地图< String,String>到POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看杰克逊,但似乎我必须将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似乎更舒服)。

Well, you can achieve that with Jackson, too. (and it seems to be more comfortable since you were considering using jackson).

使用 ObjectMapper 's 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.

这篇关于转换地图< String,String>到POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆