1:将Object类型转成json字符串
/**
* 将对象转为字符串
* @param obj
* @return
*/
public static String toString(Object obj) {
if(obj == null) {
return null;
}
if ("".equals(obj.toString())) {
return null;
}
if (obj instanceof String) {
return obj.toString();
}
try {
ObjectMapper mapper = getMapperInstance();
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(sw);
mapper.writeValue(gen, obj);
return sw.toString();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
2:将Object转成List
/**
* 转换为List
* @param string
* @param cls 为List集合里面的类型
* @return
*/
public static <T>T toList(String string, Class<?> cls) {
if(StringUtil.isEmpty(string)) {
return null;
}
if (!string.contains("{") && !string.contains("[")) {
return null;
}
try {
ObjectMapper mapper = getMapperInstance();
JavaType type = mapper.getTypeFactory().constructParametricType(List.class, cls);
return mapper.readValue(string, type);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return null;
}
3:将字符串转为对象
/**
* 将字符串转为对象
* @param string
* @param cls
* @return
*/
@SuppressWarnings("unchecked")
public static <T>T toObject(String string, Class<?> cls) {
if(StringUtil.isEmpty(string)) {
return null;
}
if (!string.contains("{") && !string.contains("[")) {
return null;
}
try {
ObjectMapper mapper = getMapperInstance();
//mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.TRUE);
return (T) mapper.readValue(string, cls);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return null;
}
4:将字符串转为JsonNode对象
/**
* 将字符串转为JsonNode对象
* @param string
* @return
*/
public static JsonNode toJsonNode(String string) {
if(StringUtil.isEmpty(string)) {
return null;
}
try {
ObjectMapper mapper = getMapperInstance();
return mapper.readTree(string);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return null;
}
5:转换为List《Map》
/**
* 转换为List<Map>
* @param string
* @return
*/
public static <T>T toListMap(String string) {
if(StringUtil.isEmpty(string)) {
return null;
}
if (!string.contains("{") && !string.contains("[")) {
return null;
}
try {
ObjectMapper mapper = getMapperInstance();
JavaType type = mapper.getTypeFactory().constructType(List.class, HashMap.class);
if (!string.startsWith(JSON_2)) {
string = URLDecoder.decode(string, "UTF-8");
}
return mapper.readValue(string, type);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
6:将字符串转为Map对象
/**
* 将字符串转为Map对象
* @param string
* @return
* @return
*/
@SuppressWarnings("unchecked")
public static <T>T toMap(String string) {
if(StringUtil.isEmpty(string)) {
return null;
}
if (!string.contains("{") && !string.contains("[")) {
return null;
}
try {
ObjectMapper mapper = getMapperInstance();
JavaType type = mapper.getTypeFactory().constructType(Map.class);
return (T) mapper.readValue(string, type);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return null;
}
类初始配置
public static final String FMT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
private static final Logger LOGGER = LoggerFactory.getLogger(FrameJsonUtil.class);
//private static final String JSON_1 = "{";
private static final String JSON_2 = "[{";
private static ObjectMapper mapper;
private static ObjectMapper getMapperInstance() {
return getMapperInstance(false);
}
private static synchronized ObjectMapper getMapperInstance(boolean createNew) {
if (createNew) {
ObjectMapper m = new ObjectMapper();
m.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
return m;
} else if (mapper == null) {
mapper = new ObjectMapper();
}
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FMT_DEFAULT);
mapper.setDateFormat(simpleDateFormat);
return mapper;
}
以上的是java对象互换工具类 若需完整代码 可识别二维码后 给您发代码。