对于一个Map我们如何把它转成对应的对象呢,其实方法很多,我之前用的比较多的是通过JSON转换,如下:
public class Person {
private String userName;
private int age;
public String getUserName() {
return userName;
}
public int getAge() {
return age;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setAge(int age) {
this.age = age;
}
}
public static void main(String[] args) {
Map<String,Object> source=new HashMap<>();
source.put("userName","张三");
source.put("age",1);
//map转为对象
Person person = JSONObject.parseObject(JSONObject.toJSONString(source), Person.class);
System.out.println(JSONObject.toJSONString(person));
}