json操作實用工具類。實用jackson實現

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jinkworld.car.core.mvc.vo.CarMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 使用jackson包
 * 
 * 1.對象轉json字符串
 * 2.字符串轉化爲對象
 * 3.字符串轉化爲ArrayList對象
 * 4.字符串轉化爲ArrayList的HashMap對象
 * 5.HashMap對象轉對象
 *
 * Created by shirman on 2016/9/22.
 */
public class JsonUtils<T> {
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);
    private static ObjectMapper objectMapper;
    private static final String TIME_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    private String timeFormat;

    public String getTimeFormat() {
        return timeFormat;
    }

    public void setTimeFormat(String timeFormat) {
        this.timeFormat = timeFormat;
        objectMapper.setDateFormat(new SimpleDateFormat(timeFormat));
    }

    public JsonUtils() {
        objectMapper = new ObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_FORMAT));
    }

    public JsonUtils(String  timeFormat) {
        objectMapper = new ObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setDateFormat(new SimpleDateFormat(timeFormat));
    }

    /**
     * 對象轉json字符串
     * @param object
     * @return
     */
    public static String toJSon(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            LOGGER.error("對象轉json字符串", e);
        }
        return "";
    }

    /**
     * 字符串轉化爲對象
     * @param v
     * @param json
     * @return
     */
    public T getObjectFromStr(Class<T> v,String json){
        try {
            return objectMapper.readValue(json.getBytes(),objectMapper.constructType(v));
        } catch (IOException e) {
            LOGGER.error("字符串轉化爲對象異常", e);
        }
        return null;
    }

    /**
     * HashMap對象轉對象
     * @param v
     * @param map
     * @return
     */
    public T getObjectFromMap(Class<T> v,HashMap<String,Object> map){
        return objectMapper.convertValue(map, objectMapper.getTypeFactory().constructType(v));
    }

    /**
     * 字符串轉化爲ArrayList對象
     * @param v
     * @param json
     * @return
     */
    public List<T> getArrayListObjectFromStr(Class<T> v, String json){
        try {
            return objectMapper.readValue(json.getBytes(), objectMapper.getTypeFactory().constructParametricType(ArrayList.class,v));
        } catch (IOException e) {
            LOGGER.error("字符串轉化爲ArrayList對象異常", e);
        }
        return null;
    }

    /**
     * 字符串轉化爲ArrayList的HashMap對象
     * @param json
     * @return
     */
    public List<T> getArrayListMapFromStr(String json){
        try {
            return objectMapper.readValue(json.getBytes(), objectMapper.getTypeFactory().constructParametricType(ArrayList.class, HashMap.class));
        } catch (IOException e) {
            LOGGER.error("字符串轉化爲ArrayList的HashMap對象異常", e);
        }
        return null;
    }

    public static void main(String[] args) {

        JsonUtils<CarMessage> jsonUtils = new JsonUtils<CarMessage>();
        CarMessage objectFromStr = jsonUtils.getObjectFromStr(CarMessage.class, "{\"type\":1,\"driverId\":1,\"driverName\":\"18868877621\",\"driverLocation\":{\"longitude\":121.581206,\"latitude\":29.864368,\"driverType\":10,\"status\":1}}");
        objectFromStr.getDriverName();
        String s = jsonUtils.toJSon(objectFromStr);
        System.out.println(s);
        CarMessage objectFromStr2 = jsonUtils.getObjectFromStr(CarMessage.class, s);
        String s1 = jsonUtils.toJSon(objectFromStr);
        Long driverId = objectFromStr2.getDriverId();
        System.out.println(s1);

        List<CarMessage> list = new ArrayList<CarMessage>();
        list.add(objectFromStr);
        list.add(objectFromStr2);
        String s3 = jsonUtils.toJSon(list);
        System.out.println(s3);
        List<CarMessage> listObjectFromStr = jsonUtils.getArrayListObjectFromStr(CarMessage.class, s3);
        System.out.println(jsonUtils.toJSon(listObjectFromStr));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章