ObjectMapper for converting java object json format data and format data JSONObject objects parse json

ObjectMapper = new new ObjectMapper ObjectMapper ();
// deserialization time if the other attributes much, do not throw an exception
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();

More knowledge reference https://www.cnblogs.com/xuwenjin/p/8976696.html

Guess you like

Origin www.cnblogs.com/jasonboren/p/11756469.html