我如何将 Object 转换为Map<String, String> where keyis obj.parameter.nameand valueisobj.parameter.value
例如:Object obj = new myObject("Joe", "Doe");转换为Map键:名称,姓氏和值:Joe,Doe。
MM们
浏览 179回答 2
2回答
不负相思意
除了使用反射的技巧解决方案外,您还可以尝试jackson如下一行:objectMapper.convertValue(o, Map.class);一个测试用例: @Test public void testConversion() { User user = new User(); System.out.println(MapHelper.convertObject(user)); } @Data static class User { String name = "Jack"; boolean male = true; }// output: you can have the right type normally// {name=Jack, male=true}