zoukankan      html  css  js  c++  java
  • com.fasterxml.jackson包序列化json对象和反序列化

    需要序列化的类需实现接口:

    public class ResponseModel implements Serializable {
    

      

    序列化和反序列化代码例子:

    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.joyce.jpa.model.ResponseModel;
    import org.junit.jupiter.api.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.IOException;
    
    public class JsonTest {
        static Logger logger = LoggerFactory.getLogger(JsonTest.class);
    
        @Test
        public void test1() throws JsonProcessingException {
            ResponseModel model = new ResponseModel();
            model.setCode("9999").setError("未知错误");
    
            String jsonStr = buildJsonStrBy(model);
            logger.info("jsonStr == " + jsonStr);
    
        }
    
        String buildJsonStrBy(ResponseModel model) throws JsonProcessingException {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    //        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
            return mapper.writeValueAsString(model);
        }
    
        ResponseModel deserializingToCouponUserBy(String jsonStr) throws IOException {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    //        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    //        mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL , false);
    //        mapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT , false);
    //        mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT , false);
    //        mapper.configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES , false);
    //        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES , false);
            return mapper.readValue(jsonStr, ResponseModel.class);
        }

    @Test
    public void test转换成List对象() throws IOException {
    String myUserJsonStr = "[{"username":"zhangsan","age":20,"money":80.0},{"username":"100498008","age":1,"money":10.0}]";
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // 可以忽略字母大小写,可以解决Json中字段首字母大写不能识别的问题
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

    CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, MyUser.class);
    List<MyUser> result = (List)mapper.readValue(myUserJsonStr, type);

    }
    }

    end.

  • 相关阅读:
    软考相关网站汇总
    教育界常用网站汇总
    vue实现选中li变色--小技巧
    vue前端路由的两种模式,hash与history的区别
    element中级联选择器动态加载数据 递归的思想(数据量过于庞大,后端不一次性把数据返回)
    关于echarts和高德地图使用的一些小细节
    Vue中的$set的使用 (为对象设置属性)
    高德地图根据地址获取经纬度
    vue强制刷新组件 (用keep-alive怎么都不生效,可能是因为这是组件,没涉及到路由),相当于每次重新初始化组件
    webpack
  • 原文地址:https://www.cnblogs.com/zhuwenjoyce/p/13339191.html
Copyright © 2011-2022 走看看