1. 程式人生 > >json操作實用工具類。實用jackson實現

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)); } }