将对象或集合转成JSON数据

需要的jar包

        
		
			com.fasterxml.jackson.core
			jackson-databind
			2.11.1
		

 工具类

package com.wc.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;

public class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    public static String getJson(Object o){
        return getJson(o,"yyyy-HH-dd HH:mm:ss");
    }

    public static String getJson(Object o ,String time){
        //关闭时间戳解析功能
        mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
        SimpleDateFormat simpleFormatter = new SimpleDateFormat(time);
        mapper.setDateFormat(simpleFormatter);
        try {
            //最后将转换好的json对象发送出去
            return mapper.writeValueAsString(o);
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;

    }


}

你可能感兴趣的:(json,蓝桥杯,linq,jquery,java)