ObjectMapper用于将java对象转换为json格式数据以及JSONObject对象解析json格式数据

ObjectMapper objectMapper = new ObjectMapper();
//反序列化的时候如果多了其他属性,不抛出异常
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    //如果是空对象的时候,不抛异常
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

             //序列化的时候序列对象的所有属性
    objectMapper.setSerializationInclusion(Include.ALWAYS); 
    
    //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))

            //将对象转换为json格式数据  
            String jsonStr = objectMapper .writeValueAsString(javabean);
           //将对象转换为字节数组
              byte[] byteArr = objectMapper .writeValueAsBytes(Javabean);

    //将json数据读进来转换为javabean对象
           Javabean javabean = objectMapper .readValue(jsonStr, javabean.class);
    
    //将javabean对象写出去转为json格式数据
    objectMapper.writeValue();

更多知识参考https://www.cnblogs.com/xuwenjin/p/8976696.html

猜你喜欢

转载自www.cnblogs.com/jasonboren/p/11756469.html