相关文章推荐

使用Jackson实现Map与Bean互转方式

作者:沧海一粟X

这篇文章主要介绍了使用Jackson实现Map与Bean互转方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Jackson Map与Bean互转

在使用 java 开发中,通常需要把 Map 转成 Bean,或把 Bean 转成 Map,这就用的工具类,在此推荐使用import com.fasterxml.jackson.databind.ObjectMapper;包下的ObjectMapper类,比 JsonObject 效率高

下面就列举了几种使用方法

1、pom.xml

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency>

2、java 代码

package com.jin.demo.jackson; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; import java.util.Map; * 使用 Jackson工具 * map to bean * bean to map * @auther jinsx * @date 2019-03-26 16:37 public class JacksonTest { private static final ObjectMapper mapper = new ObjectMapper(); public static void main(String[] args) throws Exception { // 测试 将Map转成指定的Bean Map<String, Object> map = new HashMap<>(); map.put("name", "张三"); Person o = (Person)JacksonTest.mapToBean(map, Person.class); System.out.println(o); // 测试 将Bean转成Map Person person = new Person(); person.setAge(18); Map map1 = JacksonTest.beanToMap(person); System.out.println(map1); // 将对象转成字符串 public static String objectToString(Object obj) throws Exception { return mapper.writeValueAsString(obj); // 将Map转成指定的Bean public static Object mapToBean(Map map, Class clazz) throws Exception { return mapper.readValue(objectToString(map), clazz); // 将Bean转成Map public static Map beanToMap(Object obj) throws Exception { return mapper.readValue(objectToString(obj), Map.class);

3、Person 类

package com.jin.demo.jackson; import java.io.Serializable; import java.util.Date; * @auther jinsx * @date 2019-03-26 17:30 public class Person implements Serializable { private int age; private String name; private Date birthday; public int getAge() { return age; public void setAge(int age) { this.age = age; public String getName() { return name; public void setName(String name) { this.name = name; public Date getBirthday() { return birthday; public void setBirthday(Date birthday) { this.birthday = birthday; @Override public String toString() { return "Person{" + "age=" + age + ", name='" + name + '\'' + ", birthday=" + birthday +

Jackson-ObjectMapper json字符串、bean、map、list互相转换

1、对象转json字符串

ObjectMapper mapper = new ObjectMapper(); User user = new User(); String userJson = mapper.writeValueAsString(user);

2、Map转json字符串

ObjectMapper mapper = new ObjectMapper(); Map map = new HashMap(); String json = mapper.writeValueAsString(map);

3、数组list转json字符串

ObjectMapper mapper = new ObjectMapper(); User[] uArr = {user1, user2}; String jsonfromArr = mapper.writeValueAsString(uArr);

4、json字符串转对象

ObjectMapper mapper = new ObjectMapper(); String expected = "{\"name\":\"ZhangSan\"}"; User user = mapper.readValue(expected, User.class);

5、json字符串转Map

ObjectMapper mapper = new ObjectMapper(); String expected = "{\"name\":\"ZhangSan\"}"; Map userMap = mapper.readValue(expected, Map.class);

6、json字符串转对象数组List

ObjectMapper mapper = new ObjectMapper(); String expected = "[{\"name\":\"Zhangsan\"},{\"name\":\"Lisi\"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class); List<User> userList = mapper.readValue(expected, listType);

7、json字符串转Map数组List<Map<String,Object>>

ObjectMapper mapper = new ObjectMapper(); String expected = "[{\"name\":\"Zhangsan\"},{\"nameEn\":\"Lisi\"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class); List<Map<String,Object>> userList = mapper.readValue(expected, listType);

8、jackson默认将对象转换为LinkedHashMap

ObjectMapper mapper = new ObjectMapper(); String expected = "[{\"name\":\"Zhangsan\"},{\"nameEn\":\"Lisi\"},{\"name\":\"Test\"}]"; ArrayList arrayList = mapper.readValue(expected, ArrayList.class);

9、json字符串转对象设置格式

ObjectMapper mapper = new ObjectMapper(); //设置date转换格式 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(fmt); String expected = "{\"name\":\"ZhangSan\",\"bir\":\"2021-04-15 14:00:00\"}"; User user = mapper.readValue(expected, User.class);

10、忽略未知字段

ObjectMapper mapper = new ObjectMapper(); //设置忽略未知字段 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); String expected = "{\"name\":\"ZhangSan\",\"job\":\"teacher\"}"; User user = mapper.readValue(expected, User.class);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • token工作机制及原理附Java生成token工具类
    token工作机制及原理附Java生成token工具类
    2021-12-12
  • Mybatis无法获取带有下划线前缀的字段的值问题
    Mybatis无法获取带有下划线前缀的字段的值问题
    2021-12-12
  • springmvc项目使用@Valid+BindingResult遇到的问题
    springmvc项目使用@Valid+BindingResult遇到的问题
    2021-12-12
  • SpringBoot下载文件的实现及速度对比
    SpringBoot下载文件的实现及速度对比
    2021-12-12
  • mybatisplus @Select注解中拼写动态sql异常问题的解决
    mybatisplus @Select注解中拼写动态sql异常问题的解决
    2021-12-12
  • Java如何实现读取txt文件内容并生成Word文档
    Java如何实现读取txt文件内容并生成Word文档
    2021-12-12
  • 详细解读Java Spring AOP
    详细解读Java Spring AOP
    2021-12-12
  • mybatis新增save结束后自动返回主键id详解
    mybatis新增save结束后自动返回主键id详解
    2021-12-12
  • 美国设下计谋,用娘炮文化重塑日本,已影响至中国
    美国设下计谋,用娘炮文化重塑日本,已影响至中国
    2021-11-19
  • 时空伴随者是什么意思?时空伴随者介绍
    时空伴随者是什么意思?时空伴随者介绍
    2021-11-09
  • 工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    2021-11-05
  • 2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2021-10-26
  • 电脑版 - 返回首页

    2006-2023 脚本之家 JB51.Net , All Rights Reserved.
    苏ICP备14036222号

     
    推荐文章