package com.lyms.talkonlineweb.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2019-09-06.
*/
public class JsonUtil {
/**
* 对象转换为String
* @param cls
* @return
*/
public static String obj2Str(
Object cls) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(DateUtil.getDateFormat(DateUtil.YYYY_MM_DD_HH_MM_SS));
return objectMapper.writeValueAsString(cls);
} catch (Exception e) {
}
return null;
}
/**
* json转换为map
* @param json
* @param hashMapClass
* @return
*/
public static Map<String, Object> str2Map(String json, Class<HashMap> hashMapClass) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json,hashMapClass);
} catch (Exception e) {
}
return null;
}
/**
* 接送转换为对象
* @param json
* @param clzz
* @param <T>
* @return
*/
public static <T> T str2Obj(String json, Class<T> clzz) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json,clzz);
} catch (Exception e) {
}
return null;
}
}