zoukankan      html  css  js  c++  java
  • ObjectMapper类

    前言

    ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象匹配JSON结构,反之亦然

    使用

    使用Jackson,首先需要相关的jar包。对于使用maven的,需要添加以下依赖:

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.5</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.5</version>
    </dependency>
    

      

    实例演示

    Java对象转换为JSON对象  json对象转化为java 对象

    ObjectMapper objectMapper = new ObjectMapper();  
    
            //序列化的时候序列对象的所有属性  
            objectMapper.setSerializationInclusion(Include.ALWAYS);  
    
            //反序列化的时候如果多了其他属性,不抛出异常  
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
    
            //如果是空对象的时候,不抛异常  
            objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);  
    
            //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式  
            objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);  
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))  
    

      PS:还需要注意,如果ObjectMapper的configure设置FAIL_ON_EMPTY_BEANS为false,那么对应的实体类的属性没有get方法也不会抛出异常,但是这个属性默认的是true,即必须要有get方法,这个需要注意。

    public class ObjectMapperTest {
    
        public static void main(String[] args) throws IOException {
    
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
            objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
            Person person = new Person(1, "tom", "123");
            String jsonString = objectMapper.writeValueAsString(person);
            System.out.println("JsonString: " + jsonString);
    
            Person person1 = objectMapper.readValue(jsonString, Person.class);
            System.out.println(person1.toString());
    
        }
    
    }
    

       

    Java数组对象和JSON数组对象之间的转换

     public static void getListData() {
            ObjectMapper objectMapper = new ObjectMapper();
            //Java数组转换为JSON数组
            Person person = new Person(1, "tom", "123");
            Person person1 = new Person(2, "jack", "123445");
            List<Person> personList = new ArrayList<>();
            personList.add(person);
            personList.add(person1);
            String jsonString = null;
            try {
                //Json数组转换为Java数组
                jsonString = objectMapper.writeValueAsString(personList);
                System.out.println("JsonString List: " + jsonString);
                //JavaType
                JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Person.class);
                List<Person> list = objectMapper.readValue(jsonString, javaType);
                //打印出list中的值
                for (Person person2 : list) {
                    System.out.println(person2.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

      

  • 相关阅读:
    Javascript 使用字符串
    JavaScript精简学习4(动态表单和链接处理)
    JavaScript 使用表单
    事半功倍之Javascript (2)
    jQuery隐藏按钮
    .NET线程同步之Interlocked和ReadWrite 锁线程同步——事件构造
    linux命令汇总
    好的设计能减少大量的工作
    Prism学习笔记
    编写C函数的技巧
  • 原文地址:https://www.cnblogs.com/yangxiaobai/p/13425061.html
Copyright © 2011-2022 走看看