Jackson工具类

一、

关于Jackson相关属性配置的详解,可以参见:http://a52071453.iteye.com/blog/2175398

二、自己写了一个工具类,转换bean与json以及json与list和map,

1、项目结构

Jackson工具类_第1张图片

2、pom文件:



    4.0.0

    me.ele
    jackson_demo
    1.0-SNAPSHOT

    

        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.6
        

        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.6
        

        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.6
        

        
        
            org.apache.commons
            commons-lang3
            3.7
        


    

3、bean:

package me.ele.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

/**
 * @author LZJ
 * @create 2018-10-10 13:37
 **/

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Student01 {

    private long id;

    private String stuName;

    private int stuAge;

    private String stu_Addr;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public String getStu_Addr() {
        return stu_Addr;
    }

    public void setStu_Addr(String stu_Addr) {
        this.stu_Addr = stu_Addr;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}

4、工具类:

package me.ele.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author LZJ
 * @create 2018-10-10 14:28
 **/
public class BeanJsonConvertUtil {

    private static final ObjectMapper mObjectMapper = new ObjectMapper();
    private static final ObjectMapper mObjectMapperLowerCaseWithUnderScores = new ObjectMapper();

    //在这里进行配置全局
    static {
        //有时JSON字符串中含有我们并不需要的字段,那么当对应的实体类中不含有该字段时,会抛出一个异常,告诉你有些字段(java 原始类型)没有在实体类中找到
        //设置为false即不抛出异常,并设置默认值 int->0 double->0.0
        mObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        mObjectMapperLowerCaseWithUnderScores.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //序列化时添加下划线
        mObjectMapperLowerCaseWithUnderScores.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
    }

    public static String beanToJson(Object object) {
        if (object != null) {
            try {
                return mObjectMapper.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                System.out.println("bean to json exception");
            }
        }
        return "";
    }

    public static  T jsonToBean(String json, Class classType) {
        if (StringUtils.isNotBlank(json) && classType != null) {
            try {
                return mObjectMapper.readValue(json, classType);
            } catch (IOException e) {
                System.out.println("json to bean exception");
            }
        }
        return null;
    }

    public static  List jsonToList(String json, Class classType) {
        if (StringUtils.isNotBlank(json) && classType != null) {
            try {
                return mObjectMapper.readValue(json, mObjectMapper.getTypeFactory().constructCollectionType(List.class, classType));
            } catch (IOException e) {
                System.out.println("json to list exception");
            }
        }
        return null;
    }

    public static  Map jsonToMap(String json, Class kType, Class vType) {
        if (StringUtils.isNotBlank(json)) {
            try {
                return mObjectMapper.readValue(json, mObjectMapper.getTypeFactory().constructMapType(Map.class, kType, vType));
            } catch (IOException e) {
                System.out.println("json to Map exception");
            }
        }
        return null;
    }

    public static String beanToJsonByLowerCase(Object object) {
        if (object != null) {
            try {
                return mObjectMapperLowerCaseWithUnderScores.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                System.out.println("bean to json with lowerCase exception");
            }
        }
        return "";
    }

    public static  T jsonToBeanByLowerCase(String json, Class classType) {
        if (StringUtils.isNotBlank(json)) {
            try {
                return mObjectMapperLowerCaseWithUnderScores.readValue(json, classType);
            } catch (IOException e) {
                System.out.println("json to Bean with lowerCase exception");
            }
        }
        return null;
    }

}

5、测试:

package me.ele;

import me.ele.dto.Student01;
import me.ele.util.BeanJsonConvertUtil;

/**
 * @author LZJ
 * @create 2018-10-10 13:37
 **/
public class Test {

    public static void main(String[] args) {

        System.out.println("========student01========");
        Student01 student01 = new Student01();
        student01.setId(1);
        student01.setStuName("张三");
        student01.setStuAge(19);
        student01.setStu_Addr("黑龙江");

        System.out.println(BeanJsonConvertUtil.beanToJson(student01));
        System.out.println(BeanJsonConvertUtil.beanToJsonByLowerCase(student01));
        System.out.println("--------");

        String json1 = "{\"id\":1,\"stu_name\":\"张三\",\"stu_age\":19,\"stu_addr\":\"黑龙江\"}";
        System.out.println(BeanJsonConvertUtil.jsonToBean(json1,Student01.class));
        System.out.println(BeanJsonConvertUtil.jsonToBeanByLowerCase(json1,Student01.class));
        System.out.println("--------");

        String json2 = "{\"id\":1,\"stuName\":\"张三\",\"stuAge\":19,\"stu_Addr\":\"黑龙江\"}";
        System.out.println(BeanJsonConvertUtil.jsonToBean(json2,Student01.class));
        System.out.println(BeanJsonConvertUtil.jsonToBeanByLowerCase(json2,Student01.class));
        System.out.println("--------");

        System.out.println("========student011========");
        Student01 student011 = new Student01();
        student011.setId(2);
        student011.setStuName("李四");
        System.out.println(BeanJsonConvertUtil.beanToJson(student011));
        System.out.println(BeanJsonConvertUtil.beanToJsonByLowerCase(student011));
        System.out.println("--------");

    }

}

6、运行结果:

Jackson工具类_第2张图片

 

 

你可能感兴趣的:(java基础,json)