json,map,bean相互转换
所需jar包
commons-beanutils-1.8.0.jar
commons-logging-1.2.jar
fastjson-1.2.76.jar
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public void JSONToObject(){ User user = new User(); user.setName("小玲"); user.setAge(15); //user转为json String json = JSON.toJSONString(user); //json转为user JSONObject user1 = JSON.parseObject(json); System.out.println(user1); } public void userToDBO() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { User user = new User(); user.setName("小玲"); user.setAge(15); Map map1 = new HashMap<>(); map1 = BeanUtils.describe(user);//user转为map System.out.println(map1); Map<String,Object> map = new HashMap<>(); map.put("name","小玲"); map.put("age",15); User user1 = new User(); BeanUtils.populate(user1,map);//map转为user System.out.println(user1); } |