json,map,bean相互转换
所需jar包
commons-beanutils-1.8.0.jar
commons-logging-1.2.jar
fastjson-1.2.76.jar
代码
代码语言:javascript复制 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);
废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 转载请注明原文链接:jar包工具类